3 ms·
It's really a very poor example of real-world behavior. There are cases where pointers will perform competitively against objects, and sometimes shine. Vectors
by ComputerGuru 12y ago
It's really a very poor example of real-world behavior. There are cases where pointers will perform competitively against objects, and sometimes shine.
Vectors are awesome because of contiguous memory allocation. Vectors are also horrible because of contiguous memory allocation. Really, the rule is that you have to understand what is going on beneath the data structure abstraction. When your vector starts resizing, when you don't know beforehand how many elements will be in it - that's when you start to consider using pointers instead.
In the very first line of code, he reserves space for `count` variables - not something you're always able to do. (When you don't reserve the space beforehand, the vector is created with some initial size, and then if it exceeds it, all pre-allocated storage for the vector must be copied to a new location w/ the new size - rinse and repeat as needed.)
Obviously there are always better data structures more suited to your needs, in this case a deque might come in handy (it's quite the underrepresented STL data structure).
- deleted 12y ago[deleted]