To make the program clearer and shorter, the most common constructor of the container type is the default constructor. In most programs, the default constructor can achieve the best runtime performance,
And makes containers easier to use.
When copying a container to another container, the type must match: The container type and element type must be the same.
In C ++, most types can be used as element types of containers. The container element type must meet the following two constraints:
The element type must support the value assignment operation.
Element-type objects must be able to be copied.
All built-in or composite types can be used as element types except reference types. References do not support assignment in general sense, so no element is a reference type container.
Except for the input/output (IO) standard library type (and the auto_ptr type described in section 17.1.9), all other standard library types are valid container element types.
Note: When specifying the container element as the container type, spaces must be used as follows:
Vector <vector <string> lines; // OK: space required between close>
Vector <vector <string> lines; // error:> treated as Shift Operator
Two Adjacent> symbols must be separated by spaces to show that they are two separated symbols. Otherwise, the system considers> as a single symbol, which is a right shift operator and leads to compilation errors.
Common iterator operations:
* Item-> mem item ++/-- =! =
================================================== Only supported by vector and deque operation: ========================================================== ==========
> <>=< = Item + N item-N + =-=
The C ++ language uses a pair of iterators to mark the iterator range (iterator range). The two iterators point to two elements in the same container or the next position beyond the end, respectively,
They are usually named first and last, or beg and end, used to mark a range of elements in the container.
The range of these elements is known as left-closed sive interval. The standard format is as follows:
// To be read as: Primary des first and each element up to but not including last
[First, last)
Indicates that the range starts from first to last, but does not include last. The iterator last can be equal to or point to an element after the first element,
But it cannot point to the element before the first mark.
Modify the internal status of the container or move elements in the container. This operation invalidates all iterators pointing to the moved elements, and may also invalidate other iterators. Use
The invalid iterator is not defined and may cause the same issue as the suspension pointer.
Any insert or push operations may invalidate the iterator. When writing a loop to insert elements to a vector or deque container, the program must ensure that the iterator is
Every cycle is updated.
Do not store the iterator returned by the end operation. Adding or deleting deque or elements in the vector container will invalidate the storage iterator.
The reverse_iterator iterators that address elements in reverse order also correspond to rbegin (); rend (); member functions
Read-Only (not writable) reverse iterator of the const_reverse_iterator Element
Difference_type is enough to store signed integer values of the two iterators, which can be negative.
In short, the reverse iterator traverses the container from the back to the front and reverses some related iterator operations: for example, the ++ operation on the reverse iterator directs to
The first element.
Generic programming is usually used:
Value_type element type
The left Value Type of the reference element, which is a synonym for value_type &
The constant left Value Type of the const_reference element, which is equivalent to const value_type &
When an element is added to a container, the system copies the element value to the container. Similarly, when an element is used to initialize a new container, the new container stores a copy of the original element.
The copied original value is irrelevant to the elements in the new container. After that, when the element value in the container changes, the copied original value is not affected, and vice versa.
Another alternative to subscript calculation is the at member function. This function performs similar operations as subscript operations, but if the given subscript is invalid,
The at function will throw an out_of_range exception;
Pop_front operations are usually used together with front operations to implement stack-based container processing:
While (! Ilist. Empty ()){
Process (ilist. Front (); // do something with the current top of ilist
Ilist. pop_front (); // done; remove first element
}
The return values of pop_front and pop_back functions are not the deleted element values, but void. To get the deleted element value,
The notfront or back function must be called before the element is deleted.
Use Find in combination with delete operation
String searchvalue ("Quasimodo ");
List <string >:: iterator iter =
Find (slist. Begin (), slist. End (), searchvalue );
If (ITER! = Slist. End ())
Slist. Erase (ITER );
The assignment and assign operations invalidate all iterators of the left operand container. The swap operation does not invalidate the iterator. After completing the swap operation,
Although the exchanged elements are already stored in another container, the iterator still points to the same element.
Swap functions are more efficient;
An important question about swap is that this operation does not delete or insert any elements and ensures exchange within the constant time.
The iterator does not expire because no elements are moved in the container.
The swap operation is used to exchange all elements in two containers. The types of containers to be switched must match: The operands must be containers of the same type,
The stored element types must also be the same.
The assign function must be used when the types are different.
The assign operation first deletes all elements in the container, and then inserts the new elements specified by its parameters into the container. Like copying the constructor of container elements,
If the two containers have the same type and element type, you can assign a value to another container using the value assignment operator (=. For different (or identical) Types
In the container, the element types are not the same but are compatible with each other, so the value assignment operation must use the assign function. For example, it can be implemented through the assign operation.
Assign a char * element in the vector container to the string list container.
It is important to figure out the difference between container capacity (capacity) and size (length. Size indicates the number of elements currently owned by the container;
Capacity indicates the total number of elements that can be stored by a container before a new bucket is allocated.
The value of capacity can be changed by the Reserve Function;
For vector containers, as long as capacity is not used up, no additional storage space will be allocated;
Inserting an element at the beginning or end of the deque container does not invalidate any iterator. Deleting an element at the beginning or end only causes
The iterator of the deleted element is invalid. Insert and delete operations at any other location of the deque container will invalidate all iterators pointing to the container element.
If you cannot determine which container should be used by an application, write the code and use only the operations provided by the vector and lists containers: Use the iterator,
Instead of subscript, and avoid random access to elements. In this way, you can easily change the program from a vector container to a list container when necessary.
Deque advantages for storage-limited programming: FIFO
That is, the read data is stored at the end of the queue. The first element of the queue is prioritized and deleted after processing;
If you want to match any specified character when searching for a string, the implementation is a little more complicated. For example, the following program looks for and locates the first number in name:
String numerics ("0123456789 ");
String name ("R2D2 ");
String: size_type Pos = Name. find_first_of (numerics );
Cout <"found number at index:" <POS
<"Element is" <name [POS] <Endl;
The default stack and queue are implemented based on the deque container, while the priority_queue is implemented on the vector container. When creating an adapter
The ordered container is specified as the second type parameter of the adapter to overwrite the associated basic container type:
// Empty stack implemented on top of Vector
Stack <string, vector <string> str_stk;
// Str_stk2 is implemented on top of vector and holds a copy of SVEC
Stack <string, vector <string> str_stk2 (SVEC );
The basic container associated with the stack adapter can be any sort of sequential container type. Therefore, the stack can be built on a vector, list, or deque container.
The queue adapter requires that the associated basic container must provide the push_front operation, so it can only be built on the list container, not on the vector container.
The priority_queue adapter requires random access, so it can be built on a vector or deque container, but not on a list container.