STL containers are neat, but you had just missed the most important part. It's a lot easy to make insertion or removal operations towards a STL container, the main point is, when and what containers should we use for a specific situation. Vector can be really very inefficient then you use it to make a lot of insertion operations, because then capacity is full, the vector allocates an entire new space of the space required, also, it copies the last block of space to the new, and finally it deletes the old block of space. You also should explicitly reserve a space after the vector declaration with vector::reserve with the appropriate space you need.
For insertion operations is much better to use a std::list, which perform insertion operations very efficiently without need to resize, because std::list is a linked list, so just creates a new node and link's it to the last. The main drawback of the std::list is the access time to an element and the extra space required to maintain a double-linked list, std::vector has this advantage over std::list because the access time is fast as an built-in array because it's allocated like an array.
Maybe i should make a tutorial about this important subject.
Edit: I didn't have seen part 2 then i was writing this, you specified std::reserve and other important subjects. So you already know part of what i'm talking about.


LinkBack URL
About LinkBacks




Reply With Quote





Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum