Zephilinox wrote:
some tricky shit is going on der, most stuff ends at 255/256, and odd that you would stop at 58 instead of 59, also why 0, 1 and 2? I'd expect 0 and 1, 0-9 is the only thing that makes sense to me on its own, but not when combined with the rest. |
My idea was to post an example and see how much it confused everyone - and that is what happened. I used 255 for the degrees and should have used "." as the separator in the hope that someone might have thought it was an IP address or something.
Here is the exact same thing with decent variable names:
1 2 3 4 5 6 7 8 9 10
|
int Deg =0; //Degree Counter
int Min = 0; //Minute Counter
int Sec = 0; //Seconds Counter
int Tenths = 0; //Tenths of seconds counter
for(int Deg= 255;Deg<259;Deg++)
for(int Min=55;Min<59;Min++)
for(int Sec=0;Sec<3;Sec++)
for(int Tenths=0;Tenths<10;Tenths++)
cout << Deg << ":" << Min<< ":" << Sec << ":" << Tenths <<endl;
|
See how obvious that is - even without the comments it is fairly clear. Again I always comment & initialise variables at declaration.
Someone might have guessed my original example if I had used the ranges 0-360, 0-60, 0-60 & 0-10, however that would have produced 360*60*60*10 lines of output.
Volatile Pulse wrote:
Now that one isn't so bad, I didn't think of a very long nested for loop, but the idea is that using descriptive for variables is nice, but it can make the code extremely long. I would simply use i, x, y here and just add a comment at the for loop describing what each loop does. |
The coding std that I try to stick to says that variable names shouldn't be longer than 10 chars, most of the time I can do 6 or 7 which is reasonable. I find this way strikes a balance between meaningful & not too long code.
I would nearly always use Row, Col for 2D arrays just like your example, for the reasons I mentioned earlier.
I once saw someone doing code with matrices - there were i's & j's everywhere. |
That example was a large 2D array with a 5 by 5 array being used to do matrix operations on each 5 by 5 chunk of the larger array. It is easy to imagine how that can go astray.
I guess it does boil down to personal preference, and particularly the good use of comments.
Volatile Pulse wrote:
Instead of just pushing back your people in the loop, you should set the size of the vector upon construction. This will prevent the vector from constantly having to find memory to allocate for each person added. |
In my understanding, vectors have a default capacity (worthwhile to find out what that is on your system, (same for me)). I have a vague idea that is is a reasonable size, so one can push_back happily for a quite awhile without any performance issues. The other thing is push_back places things at the end of the vector, so if you were to create a vector with 20 initialised values (0 say), then push_back something, you will still have 20 zero's at the start. I find it easy to create an empty vector, then push things into it. You can of course put things in via the array index, but I like the push_back better.
A further thing is to decide which is better for your situation, <vector>'s or <list>'s. A vector is just like an array that can be resized. Resizing is very inefficient, it copies the whole thing somewhere else in memory. There are strategies for resizing such as doubling the size each time so as not to resize very often ( I learnt this the other week from Peter87). I still think that, if the vector is going to grow a lot, you are better off with a <list>. Being able to use an array index on the vector is often very convenient.
A <list> is a double linked list, so adding something anywhere is achieved by manipulating the item pointers (internally), so this is very efficient. There is some overhead when using small object like an int, because the list has to store 2 pointers for each object, so anything bigger than 3 ints would be ok (not hard to come across in real world)
So, I like to use vectors if they are not going to change in size much, and a list if they are, and a <map> (efficient for find & sort) if there are going to be a large number of them. A worst case scenario might be a vector of vectors - that would be at least 2N * 2M efficiency if both had to be resized - really bad.
As an example, I am having a go at the Sum of Product problem on Online Judge. It involves operations on sets of permutations. I made a permutation a <vector> , and I decided I wanted to keep some of them to aid my analysis of the problem. So I made a <list> of the permutation <vector>'s
Any way that's enough of rambling on and on in some one else's thread, hope it is useful.