There’s a well known image (cheat sheet) called “C++ Container choice”. It’s a flow chart to choose the best container for the wanted usage.
Does anybody know if there’s already a C++11 version of it?
This is the previous one:

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Not that I know of, however it can be done textually I guess. Also, the chart is slightly off, because
listis not such a good container in general, and neither isforward_list. Both lists are very specialized containers for niche applications.To build such a chart, you just need two simple guidelines:
Worrying about performance is usually useless at first. The big O considerations only really kick in when you start handling a few thousands (or more) of items.
There are two big categories of containers:
findoperationand then you can build several adapters on top of them:
stack,queue,priority_queue. I will leave the adapters out here, they are sufficiently specialized to be recognizable.Question 1: Associative ?
Question 1.1: Ordered ?
unordered_container, otherwise use its traditional ordered counterpart.Question 1.2: Separate Key ?
map, otherwise use asetQuestion 1.3: Duplicates ?
multi, otherwise do not.Example:
Suppose that I have several persons with a unique ID associated to them, and I would like to retrieve a person data from its ID as simply as possible.
findfunction, thus an associative container1.1. I couldn’t care less about order, thus an
unordered_container1.2. My key (ID) is separate from the value it is associated with, thus a
map1.3. The ID is unique, thus no duplicate should creep in.
The final answer is:
std::unordered_map<ID, PersonData>.Question 2: Memory stable ?
listQuestion 2.1: Which ?
list; aforward_listis only useful for lesser memory footprint.Question 3: Dynamically sized ?
{ ... }syntax), then use anarray. It replaces the traditional C-array, but with convenient functions.Question 4: Double-ended ?
deque, otherwise use avector.You will note that, by default, unless you need an associative container, your choice will be a
vector. It turns out it is also Sutter and Stroustrup’s recommendation.