Chapter 9 Sequential Containers
Overview of the Sequential Containers
- Sequential container types to choose:

Container Library Overview
- Container operations overview:

- Container constructors overview:

- A reverse iterator goes backward and inverts the meaning of iterator operations.
- Containers provide type aliases for elements.
value_type,reference, andconst_referenceare all correspondent to the real type, and are useful in generic programs. begin()andend()are overloaded with aconst(equivalent tocbegin()) and a non-constversion. Using which version depends on whether the object isconst.- Direct copying a container for initialization requires identical container types. Otherwise, use a pair of range iterators (the element types don’t need to be identical either, as long as they can convert).
- Arrays are declared to be of a fixed size:
array<int, 10>. In its default constructor, elements are default initialized. If it’s list initialized, trailing elements will be value initialized (the same as built-in arrays). - Container assignment operations modify the entire container:

- Unlike built-in arrays, arrays can be copied or assigned (but not with a braced list of values).
- Assignment with
=requires that operands have the same type. Otherwise, useassign()to support conversion. swap()does not copy elements (except forarray) but swap internal data structures and pointers to the memory. It takes constant time. As a result, iterators, references and pointer are not invalidated and still point to the previous data field.- Containers support
==/!=and>/>=/</<=(except for unordered associative containers) between same types of containers. They work similarly tostringrelationals and use the elements’ relational operator==and<.
Sequential Container Operations
- Insertion operations for sequential containers:
- When we call a
pushorinsertmember, we pass objects of the element type and those objects are copied into the container. When we call anemplacemember, we pass arguments to a constructor for the element type. The elements are constructed directly in place (avoid creating temporary objects).
- When we call a

- Access operations for sequential containers:
- Access operations return references. If the container is
const, then return is a reference toconst. atensures the index is valid.
- Access operations return references. If the container is

- Remove operations for sequential containers:

forward_listimplements a singly linked list, so it has a different set of insert/remove operations:

resize(n)orresize(n, t)resize containers by initializing additional elements (value initialization if no value is given) or discarding excess elements.- Operations that add or remove elements from a container can invalidate pointers, references or iterators. They behave intuitively and match the implementation of data structures.
end()is always invalidated when we add or remove elements in avector,stringordeque(remove any but the first element). Thus,end()should be called every time the container is resized.- Note:
dequeis actually implemented as a map of chunks of fixed size vector.

How a vector Grows
shrink_to_fit(),capacity(),reserve(n)(at leastn) can be used to manage and access the capacity of containers (vectororstring,shrink_to_fit()also applies todeque).shrink_to_fitis only a request; there is no guarantee.
Additional string Operations
- Three additional ways to construct
strings with slicing:

s.substr(pos = 0, n = npos)returns a substring.- Various operations to modify
strings:appendis a shorthand way of callinginsert, andreplaceis a shorthand way of callingeraseandinsert.- Common interface:
- Specify the range of characters to remove: position and length, iterator range.
- Specify the insertion point: index, iterator.
- Specify the characters to add: another
string,charpointer, brace-enclosed list of characters, character and count. The former two ways also support substrings (index, length).

- Six search functions with four overloaded versions each:
- Returns
nposif not found (-1in signed).
- Returns

compare()member function behaves similarly asstrcmp. It accepts arguments in six formats:

- Conversions between
strings and numbers:- The first non-whitespace character in the
stringmust be a character that can appear in a number. The function will read thestringuntil it finds a character not in a number.
- The first non-whitespace character in the

Container Adaptors
stack,queue, andpriority_queueare sequential container adaptors that make containers act like different types.- By default,
stackandqueueare implemented on adeque, andpriority_queueis implemented on avector. It can be overridden with the second type argument at creation. emplace()also applies to these adaptors and can be used to insert objects constructed from an initializer list quickly.