Examples
Team LiB
Previous Section Next Section

Examples

Example: vector for small lists. Is it a common fallacy to use list just because "list is obviously the right type for list operations," such as insertion into the middle of a sequence. Using a vector for small lists is almost always superior to using list. Even though insertion in the middle of the sequence is a linear-time operation for vector and a constant-time operation for list, vector usually outperforms list when containers are relatively small because of its better constant factor, and list's Big-Oh advantage doesn't kick in until data sizes get larger.

Here, use vector unless the data sizes warrant a different choice (see Item 7), or because the strong safety guarantee is essential and the contained type's copy constructor or copy assignment operator might fail (in which case, it can be important that list provides the strong guarantee for insert operations for collections of such types).

    Team LiB
    Previous Section Next Section