2-d constructs can be trouble or beautiful.
Pros: they stretch in both directions as a vector built, they are easy to index in human readable format, easy to understand and work with.
cons: performance, across the board, can get nasty with big ones. Every time you grab one, you call number of rows constructors and destructors for the internal vectors. Each of those is a new/delete pair at least once, and possibly many times if the vectors grow to fit some unknown size. Also, copying is more complex, it has to copy each internal vector one by one. Also, it fragments the memory across the rows; each row can be in a different page of memory and cause massive page faulting. Those are just a few examples of how they bite you. Consider 1-d if any of this concerns you. As small as this is, it will not matter; but if you use the same ideas in a big program, it will.
it may be better to use arrays than vectors, maybe pointers, iterators in exchange for loops, and so. An informed opinion would be very useful.
|
the small speed gain of arrays is not usually worth the hassle. I use arrays because I learned the language before vector existed and often, it is true, I do not need the capability of vectors. But this is just me being old and set in my ways. Use vectors. Pointers: use pointers as much as you like. But do not use dynamic memory directly at all if you can possibly avoid it. In general, use a reference where a reference will do, and a pointer if it will not, and a container instead of dynamic memory unless you have a really hard core argument against it (like, you insist on having a classical pointer based tree construct). Iterators are used because you use stl containers :) I find them awkward, but you can't really live without them either, so get used to it and use the darn things.
Iterators do not replace loops, they honestly are closest to an abstraction of a pointer, consider:
int x[100];
int *ip = x;
for(; ip<ip+100; ip++) *ip= rand();
that ^^^ looks an awful lot like an iterator, right? But you still have to loop. Calling <algorithms> that eat iterators for parameters is still a loop, its just that the loop is in code you did not write now. Hiding a loop does not eliminate it. You should use <algorithms> and iterators etc; I am not saying to not use them just trying to show that you can't replace a loop if you want something looped -- somewhere, somehow, something has to cycle whether its recursion, unrolled, in a library, or whatever.