There is usually no special reason to use vectors.
The list and forward_list have additional memory overhead, and if there are many small elements, do not use them.
If the element needs to be inserted in the middle of the container only when reading the input, then random access is required.
1 Determine if it is really necessary to insert in the middle position, you can use the vector and sort;
2 If the element must be inserted in the middle, you can use the list in the input stage, and then copy to the vector
Insert and Emplace (Emplace_back, Emplace_front)
Emplace is a constructor that directly passes parameters to the element type and constructs the element directly in the container-managed memory space.
It eliminates the construction of temporary objects and reduces memory overhead when compared to insert.
type aliases are useful in generic programming.
Direct container copy , which requires both the container type and the element type to match;
Using an iterator copy is not required, as long as the element type can be converted.
Array, as with the built-in array, is also part of the type.
Unlike other containers, the elements of an array that is constructed by default are initialized by default, as with arrays.
However, the array supports copy and assignment operations, but the container type and size must be the same as the element type.
The curly braces list is not allowed to be assigned to an array.
Assign allows you to assign a value from a different but compatible type
The assign operation is not applicable with the array and associated containers.
The swap operation does not copy, delete, or insert any elements (except for the array), which can be done in constant time.
Pointers and references are not invalidated after the swap, except for iterators that point to the container outside of string. Still points to the element before the swap operation, but it already belongs to a different container.
For Array,swap to actually exchange elements, the time required is proportional to the number of elements. However, the iterator pointers are not invalidated.
Non-member version swap is important in generic programming.
Comparison operators can be used to compare two containers only if the element also defines the corresponding comparison operator.
Inserting elements (except the end-to-end) into vectors, strings, and deque requires moving elements and may even cause the reallocation of the object's storage space. When inserting with an iterator range, the iterator cannot point to this container.
accessing elements
Ensure non-null before calling front or back (dereference begin--end).
C.front () C.baock () C[n] all have an out-of-bounds risk c.at (n) throws a Out_of_range exception.
Forward_list Special operation
Because the unidirectional list does not have an easy way to get its precursor, it is done by manipulating the elements that follow the given element. Insert_after, Emplace_after, Erase_after.
Also deliberately defines the first pre-iterator Lst.before_begin () Lst.cbefore_begin ()
The general Insert/emplace returns an iterator to the first added element, while the after version points to the last iterator that inserted the element.
Iterator invalidation
adding elements
Vector, string: The storage space is redistributed, all is invalidated, and the iterator that inserted the previous part, the reference pointer is still valid, is not reassigned.
Deque: All positions outside the end are not valid. The iterator is invalidated and the reference pointer does not.
List, forward: still valid.
Delete Element
List, forward: still valid.
Deque: All positions outside the end are not valid. Delete the tail element, after the end of the iterator invalidation; Delete the first element, still valid
Vector, string: the part before being deleted is still valid.
When using iterators (pointers, references), minimizing a program fragment that requires an iterator to be valid. Updates the iterator in the loop.
Post-tail iterators are always invalidated and do not save
Capacity Size
Shrink_to_fit () reduces allocated space to the same size as size (), only for vector, string, deque
Capacity () does not reallocate memory, how many elements can be saved, only vector and string
Reserve () Allocates a memory space of at least n elements, vector and string only
Resize () changes the number of elements in the container without reducing the amount of memory space reserved by the container
String Extra Action
Structure
If you construct a string from an array, you must end with a null character, or you need to specify the first n characters of the copy.
string s (CP, N);;
String s (S1, Pos,len), POS exceeds S1 size will report an exception, Len no matter how large the maximum copy to the end of a string.
String, in addition to the iterator version, supports subscript operations
Insert, Erase, append, replace, assign
search Function
return String::size_type unsigned type
It is not a good idea to save the return value with int.
Find (args)//find where args first appears in S
RFind (args) rewind//Find the last occurrence of args in S
Find_first_of (args)//s find the first occurrence of any character in args
Find_last_of (args)//s to find the last occurrence of any of the characters in args
Find_first_not_of (args)//s to find the first character not in args
Find_last_not_of (args)//s to find the last character not in args
In args you can specify the location POS to start looking for, which defaults to 0. This can be conveniently searched continuously.
Numeric type conversions
To_string (Val)
Stoi (S, p, b)
STOL (S, p, b)
Stoul (S, p, b)
Stoll (S, p, b)
Stoull (S, p, b)
Stof (S, p, b)
Stod (S, p, b)
Stold (S, p, b)
P is a pointer to the size_t that holds the subscript for the first non-numeric character, which defaults to 0
B is the base of the conversion, the default is 10
Container Adapter
An adapter is a mechanism that makes the behavior of something look like something else. Containers, iterators, and functions all have adapters. Universal operation empty, size, swap
By default, stack and queue are based on the vector implementation of Deque,priority_queue.
Stack: Requires only push_back pop_back back, except for array and forward_list
Queue: Requires back, push_back, front, Pop_front, can be constructed with list or deque
Priority_queue: In addition to front, push_back, pop_back operation also requires random access, can be constructed in vector or deque.
Stack stack operations
S.pop ()
S.push (item)
S.emplace (args)
S.top ()
Queue and Priority_queue operations
Q.pop () returns the highest priority element of the queue first element or Prio
Q.front () Q.back () returns the first element or tail element, not deleted, only for queue
Q.top () returns the highest-priority element without deleting
Q.push (item) creates an element at the end of the queue or prio, or constructs an element from args
Q.emplace (args)
Prefix infix suffix expression
http://blog.csdn.net/yuyanggo/article/details/48063473
Basic----sequential containers for C + +