random tips and tricks that might be useful

1. have you seen unindented codes from the forum?
well if you're using code::blocks, there is a plugin called AStyle aka Source Code Formatter.. just goto plugins>source code formatter then is should format your code in ANSI style..

you can also choose indenting styles by going settings>editor>source formatter.

2. if you're testing a program and it needs input from the console, you can save your input in a text file then run your program like this. program.exe<file.txt

1
2
3
4
5
6
7
8
9
10
11
//main.exe
#include <iostream>
using namespace std;

int main() {
    int num1, num2;
    cin >> num1;
    cin >> num2;
    cout << "the sum is: " << num1+num2 << endl;
    return 0;
}
[input.txt]
6
7

-------------------------------
the sum is: 13

in this case we run our program like main.exe<input.txt
note:: windows only!

3. you might want code::blocks to let you know if your code is going too long.
it's a good idea not to make very long statements right? so here goes
settings>editor>margins and caret>right margin hint then select visible line

that's it for now guys.. i'll post again later. if you have your own tip. don't hesitate to post it here
Last edited on
#2 works also on Unix-like systems
loops, iterators, for_each.

Here are 3 examples. They do the same thing, but using different implementations
1
2
3
4
5
6
7
8
9
10
11
12
class Window
{
public:
    virtual void Show(bool isShow) ;
};

std::vector<Window*> windows;
......//filling array
for(int i = 0; i < windows.size(); ++i)
{
    windows[i]->Show(false);
}

1
2
3
4
5
6
7
8
9
10
11
12
class Window
{
public:
    virtual void Show(bool isShow) ;
};

std::vector<Window*> windows;
......//filling array
for(std::vector<Window*>::iterator it = windows.begin(); it != windows.end(); ++it)
{
    (*it)->Show(false);
}

1
2
3
4
5
6
7
8
9
class Window
{
public:
    virtual void Show(bool isShow) ;
};

std::vector<Window*> windows;
......//filling array
std::for_each(windows.begin(), windows.end(), std::bind2st(std::mem_fun(&Window::Show), false));

performance optimization using 'if' statement.

Old example
1
2
3
4
5
6
bool lightCheck();//this function is very simple and doesn't require many processor time
bool hardCheck();//this function is very difficult, it has loops, recursion, and other resource required operations
if (hardCheck() && lightCheck())
{
     //do something if both operations are successes. 
}

New example
1
2
3
4
5
6
bool lightCheck();//this function is very simple and doesn't require many processor time
bool hardCheck();//this function is very difficult, it has loops, recursion, and other resource required operations
if (lightCheck() && hardCheck())
{
     //do something if both operations are successes. 
}


As you can see the both example do the same. But if statement has another order.
According to C++ standard statements in 'if' condition calculates from left to right.

So suppose we have condition when
1
2
bool res1 =  lightCheck(); //res1 == false
bool res2 =  hardCheck(); //res2 == true 


And in the old example we call hardCheck and then lightCheck.
In new example we call only lightCheck().

If the situation is another
1
2
bool res1 =  lightCheck(); //res1 == true
bool res2 =  hardCheck(); //res2 == false 

Then hardCheck() will be called in both examples, but lightCheck() will be called only in new example.
But as we now lightCheck() doesn't have overhead, so it's ok.

And the another example, it's the same for performance but for difficulty of reading code is another.

Old
1
2
3
4
5
6
7
8
SomeType* ptr = init();
if (ptr)
{
    if (ptr->get() == true)
    {
        //do something
    }
}

New
1
2
3
4
5
6

SomeType* ptr = init();
if (ptr && ptr->get() == true)
{
    //do something
}
Last edited on
#2 works also on Unix-like systems
oh! sorry i didn't know.

thanks guys for posting your tricks..
For those using boost:

1
2
3
4
5
6
7
8
9
10
class Window
{
public:
    virtual void Show(bool isShow) ;
};

std::vector<Window*> windows;
......//filling array
std::for_each(windows.begin(), windows.end(), 
    boost::bind( &Window::Show, _1, false ) );
I don't like the third library if I can do the same thing by similar implementation.
And another advice.

how often do you use:

1
2
3
4
5
6
7
SomeType* ptr;
//....
if (ptr)
{
    delete ptr;
    ptr = NULL;
}

?

from standard:
if the value of the operand of delete is the null pointer the operation
has no effect.

So you can just say
 
delete NULL;

And I can change example to this
1
2
3
4
SomeType* ptr;
//....
delete ptr;
ptr = NULL;

Last edited on
If someone has a tip or trick that is useful then I think that it should be an article by itself. This kind of an open ended article will be difficult to use and the title says nothing about what subject matter might be added. The articles database is already a random collection of tips and tricks where each article contains a specific subject matter.
well if you're using code::blocks, there is a plugin called AStyle aka Source Code Formatter.. just goto plugins>source code formatter then is should format your code in ANSI style..

You can also use GNU indent if you don't have Code::Blocks.
Topic archived. No new replies allowed.