1.There are a few rules of thumb that apply to selecting which container to use:
• Unless you have a reason to use another container, use avector.
• If your program has lots of small elements and space overhead matters, don’t
use list or forward_list.
• If the program requires random access to elements, use avector or a deque.
• If the program needs to insert or delete elements in the middle of the container,use alist or forward_list.
• If the program needs to insert or delete elements at the front and the back, butnot in the middle, use adeque.
• If the program needs to insert elements in the middle of the container onlywhile reading input, and subsequently needs random access to the elements:
– First, decide whether you actually need to add elements in the middle of acontainer. It is often easier to append to avector and then call the librarysortfunction (which we shall cover in §10.2.3(p. 384)) to reorder thecontainer when you’re done with input.
– If you must insert into the middle, consider using alist for the input phase.Once the input is complete, copy thelist into a vector.
2.To create a container as a copy of another container, the container and elementtypes must match. When we pass iterators, there is no requirement that the containertypes be identical. Moreover, the element types in the new and original containers candiffer as long as it is possible to convert (§4.11, p.159) the elements we’re copyingto the element type of the container we are initializing.
When we initialize a container as a copy of another container, the containertype and element type of both containers must be identical.
The constructors that take a size are valid only for sequential containers; theyare not supported for the associative containers.
3.The fact that elements are not moved means that, with the exception ofstring,iterators, references, and pointers into the containers are not invalidated. They referto the same elements as they did before the swap. However, after the swap, thoseelements are in a different container.
Differently from the containers, a call toswap on astringmay invalidate iterators, references and pointers.
Unlike how swap behaves for the other containers, swapping twoarrays doesexchange the elements. As a result, swapping two arrays requires time proportionalto the number of elements in thearray.
After the swap, pointers, references, and iterators remain bound to the same element they denoted before the swap. Of course, the value of that element has been swapped with the corresponding element in the other array.
4.push_back:
list, vector, deque. Although string supports the 'char' version of push_back, I may simply use '+'.
5.push_front:
list, forward_list, deck
6.insert:
7.The new standard introduced three new members—emplace_front, emplace, andemplace_back—that construct rather than copy elements. These operationscorrespond to the push_front, insert, and push_back operations in that theylet us put an element at the front of the container, in front of a given position, or atthe back of the container, respectively.
8.Each sequential container, including array, has a front member, and all except forward_list also have a back member. These operations return a reference tothe first and last element, respectively .
As usual, if we use auto to store the return from one of these functions and we wantto use that variable to change the element, we must remember to define our variableas a reference type.