Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not that I know of, however it can be done textually I guess. Also, the chart is slightly off, because <code>list</code> is not such a good container in general, and neither is <code>forward_list</code>. Both lists are very specialized containers for niche applications.</p> <p>To build such a chart, you just need two simple guidelines:</p> <ul> <li>Choose for semantics first</li> <li>When several choices are available, go for the simplest</li> </ul> <p>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.</p> <p>There are two big categories of containers:</p> <ul> <li><strong>Associative</strong> containers: they have a <code>find</code> operation</li> <li><strong>Simple Sequence</strong> containers</li> </ul> <p>and then you can build several adapters on top of them: <code>stack</code>, <code>queue</code>, <code>priority_queue</code>. I will leave the adapters out here, they are sufficiently specialized to be recognizable.</p> <hr> <p>Question 1: <strong>Associative</strong> ?</p> <ul> <li>If you need to easily search by <strong>one</strong> key, then you need an associative container</li> <li>If you need to have the elements sorted, then you need an ordered associative container</li> <li>Otherwise, jump to the question 2.</li> </ul> <p>Question 1.1: <strong>Ordered</strong> ?</p> <ul> <li>If you do not need a specific order, use an <code>unordered_</code> container, otherwise use its traditional ordered counterpart.</li> </ul> <p>Question 1.2: <strong>Separate Key</strong> ?</p> <ul> <li>If the key is separate from the value, use a <code>map</code>, otherwise use a <code>set</code></li> </ul> <p>Question 1.3: <strong>Duplicates</strong> ?</p> <ul> <li>If you want to keep duplicates, use a <code>multi</code>, otherwise do not.</li> </ul> <p>Example:</p> <p>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.</p> <ol> <li><p>I want a <code>find</code> function, thus an associative container</p> <p>1.1. I couldn't care less about order, thus an <code>unordered_</code> container</p> <p>1.2. My key (ID) is separate from the value it is associated with, thus a <code>map</code></p> <p>1.3. The ID is unique, thus no duplicate should creep in.</p></li> </ol> <p>The final answer is: <code>std::unordered_map&lt;ID, PersonData&gt;</code>.</p> <hr> <p>Question 2: <strong>Memory stable</strong> ?</p> <ul> <li>If the elements should be stable in memory (ie, they should not move around when the container itself is modified), then use some <code>list</code></li> <li>Otherwise, jump to question 3.</li> </ul> <p>Question 2.1: <strong>Which</strong> ?</p> <ul> <li>Settle for a <code>list</code>; a <code>forward_list</code> is only useful for lesser memory footprint.</li> </ul> <hr> <p>Question 3: <strong>Dynamically sized</strong> ?</p> <ul> <li>If the container has a known size (at compilation time), <em>and</em> this size will not be altered during the course of the program, <em>and</em> the elements are default constructible <em>or</em> you can provide a full initialization list (using the <code>{ ... }</code> syntax), then use an <code>array</code>. It replaces the traditional C-array, but with convenient functions.</li> <li>Otherwise, jump to question 4.</li> </ul> <hr> <p>Question 4: <strong>Double-ended</strong> ?</p> <ul> <li>If you wish to be able to remove items from both the front and back, then use a <code>deque</code>, otherwise use a <code>vector</code>.</li> </ul> <hr> <p>You will note that, by default, unless you need an associative container, your choice will be a <code>vector</code>. It turns out it is also <a href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-sl11-prefer-using-stl-vector-by-default-unless-you-have-a-reason-to-use-a-different-container" rel="noreferrer">Sutter and Stroustrup's recommendation</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload