Containers (Modern C++)

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
1 out of 2 rated this helpful - Rate this topic
Visual Studio 2012

By default, use vector as the default sequential container in C++. This is the equivalent of List<T> in other languages.

vector<widget> v;
v.push_back( “Geddy Lee” );

Use map (not unordered_map) as the default associative container. Use set, multimap, multiset for degenerate & multi cases.

map<string, string> phone_book;
phone_book[“Alex Lifeson”] = “+1 (416) 555-1212”;

When performance optimization is needed, consider using:

  1. the array type when embedding is important, e.g., as a class member.

  2. unordered_map, et al.: Lower per-element overhead (major) and constant-time lookup (minor because O(log N) INVALID USE OF SYMBOLS O(K)). Harder to use correctly and efficiently, because of inconveniences + sharp edges.

  3. Sorted vector. (See: Algorithms.)

Don’t use C arrays. (For older APIs, use f( vec.data(), vec.size() ); .)

Did you find this helpful?
(1500 characters remaining)

Community Additions

© 2013 Microsoft. All rights reserved.