29

What are the general use cases for the C++ standard library containers?

  • bitset
  • deque
  • list
  • map
  • multimap
  • multiset
  • priority_queue
  • queue
  • set
  • stack
  • vector

For example, a map is generally better for a paired search.

sbi
  • 219,715
  • 46
  • 258
  • 445
Elpezmuerto
  • 5,391
  • 20
  • 65
  • 79
  • 1
    This is a duplicate of [In which scenario do I use a particular STL container?](https://stackoverflow.com/questions/471432/in-which-scenario-do-i-use-a-particular-stl-container) - the accepted answers in both use the exact same image, and that one was earlier, _and_ has more answers, so... There doesn't seem to be any need to have 2. – underscore_d Aug 02 '17 at 13:47

2 Answers2

93

A picture is worth a thousand words.

container choice flowchart

It's available from nolyc, the informative bot of ##C++ on Freenode, using the command "container choice" or "containerchoice". The link to this picture you receive in response is hosted at adrinael.net, which suggests we should thank Adrinael, member of Freenode's ##C++ community.

Tomasz Łazarowicz
  • 435
  • 1
  • 10
  • 19
  • 1
    Very useful picture, pretty much exactly what I was looking for – Elpezmuerto Oct 24 '10 at 20:00
  • 2
    Do you have an updated version of this to reflect the new C++11 containers? – Arbalest Sep 22 '13 at 00:18
  • 1
    @Arbalest [This other question](https://stackoverflow.com/questions/471432/in-which-scenario-do-i-use-a-particular-stl-container), of which the present one should be flagged as a duplicate, does, at [this answer](https://stackoverflow.com/a/22671607/2757035) – underscore_d Aug 02 '17 at 13:53
  • ...as well as having [an accepted answer](https://stackoverflow.com/a/471461/2757035) that also posts the exact same picture as this one, albeit claiming a different source for it. I don't think that hosting an image for a bot is a useful indication of authorship. So, I'm going to go out on a limb and say it's more likely that the real source/person to thank for this is David J Moore, here: http://homepages.e3.net.nz/~djm/cppcontainers.html – underscore_d Aug 02 '17 at 13:58
  • mmm this diagram does not really answer all cases. Like the first question "is order important"? Yes but I also need to find elements by key. Where do I go there? – KansaiRobot May 11 '20 at 03:13
14

bitset - used to store bits. General purpose - store some flags' values. You don't need more that 1 bit for that.

deque - double ended queue - push_back, push_front, pop_back and pop_front - basic class' methods. "Not sorted" (unordered) container.

list - linked list. This container is not memory-continuous. Its time for adding and deleting elements is O(1), but looking for a specific element is O(n). Unordered container.

map - container, stores pairs (std::pair). The first one is the key - every element from the map must be with unique key. The map is represented as tree, so the searching for an element in the map is n*log(n). This container is always sorted, that's why adding and removing elements could cause more time - the tree(the data structure) is binary and balanced.

multimap - almost the same as std::map, but allows pairs with the same keys. For example, a multimap could contain elements: (666, "alabala"), (666, "asdfg"), while the standard std::map can't. This container is also sorted.

multiset - again - the same as set, but with repeatable elements. set - well, this is also always sorted STL container. For example, a set is { 1, 2, 3 } and when you try to add '1' into this set, it will not be added, as already there's such element. (it's analogical to the mathematical's set). So, multiset allows several elements with the same value, for example { 1, 1, 1, 2, 3, 4, 4, 4, 4 } is a correct multiset, while it's not a set. Adding and removing element into std::set is still logarithmic time, as it's represented as a binary, sorted and balanced tree.

priority_queue - its first element is always the greatest of the elements it contains, according to some strict weak ordering condition. Basic functionality - push_back and pop_back.

queue - FIFO structure - First in, first out. (or the same as LILO - Last In - Last Out). It's analogue to a standard queue - when you go to a shop and start waiting on the queue, the first one there will be the first one to go. You can just push_back and pop_front. Unordered container.

set - I already described it in the multiset section.

stack - LIFO - Last In - First Out - stack. Basic functionality - push_back, pop_back. Unordered container.

vector - analogue to a standard c++ array. It's treated as regular array, it's memory-continuous, could be passed to a C program(passing the address of the first element). Unordered container.

IMPORTANT NOTE: I described the basic functionality, not the whole one. Read CPlusPlus.com for more information.

Elpezmuerto
  • 5,391
  • 20
  • 65
  • 79
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • 2
    I don't know how things were in 2010, but nowadays I see people advising to use [cppreference.com](http://en.cppreference.com/w/) instead of cplusplus.com, due to a feeling that _cppreference_ has better quality. The equivalent link at _cppreference_ is: [Containers library](http://en.cppreference.com/w/cpp/container) Btw, I can guarantee that no one in this thread is using the STL; they're using the Standard Library, which just happens to have adapted a lot from the STL. See http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about/5205571#5205571 – underscore_d Aug 02 '17 at 20:15
  • @underscore_d - yes, cppreference.com is definitely the better source. I don't use cplusplus.com anymore, it's.. let's say it this way - I wouldn't recommend it. I also agree for the STL and standard library. – Kiril Kirov Aug 03 '17 at 07:00