- Define and initialize
#include <vector>
#include <list>
#inlcude <deque>
Initialization
C<t> C; |
Empty containers, such as vector<int> Ivec; |
C c (C2); |
Create a copy of the container C2 C |
C c (b,e); |
A copy of the elements in the range identified by the iterator b,e, such as List<int> IList (Ivec.begin (), Ivec.end ()); |
C c (n,t); |
Create a container c with n values of t elements |
c c (n); |
Create a container with n-Value initialization elements |
string strings[]={"abc", "Def", Ghi "};
deque<string> Sdeq (strings,strings+sizeof (Strings)/sizeof (string));
the container element type must meet the following two conditions:
element types must support assignment operations;
an object of the element type must be replicable;
all built-in types or composite types can be used as element types in addition to reference types. (References do not support assignment operations in the general sense), and the IO type does not support copying or assigning values, so it is not possible to create containers that hold IO type objects.
a container holds a replica, and changing its value does not affect the original value being copied for containers initialized by other containers or objects.
Container container :
vector<vector<string> > lines;
>> Middle to use spaces:
vector<vector<string>> Lines;//error
- Iterators
An iterator provides an operation for all standard library container types:
*iter Iter->item ++iter Iter1==iter2 iter1!=iter2
Additional operations provided by iterators of vectors and deque containers: (Iterator arithmetic operations)
ITER + N
Iter-n
Iter1+=iter2
>,>=,<,<=
These are used only for vectors and deque containers, because only these two containers provide fast, random access to their elements.
- container Operations
container-defined type aliases:
Size_type |
UL, maximum possible length, as the type of index |
Iterator |
The type of iterator for this container type |
Const_iterator |
Read-only iterator |
Reverse_iterator |
Iterator addressing elements in reverse order |
Const_reverse_iterator |
Read-only reverse iterator |
Difference_type |
A signed integer sufficient to store two iterator differences, negative |
Value_type |
Element type |
Reference |
Element lvalue type, which is a synonym for value_type& |
const_reference |
element constant Lvalue type, equivalent to const value_type& |
Relational operators:
All containers support relational operators to implement a container comparison. The container that you compare must have the same container type, and the element type will be the same. The comparison of containers is based on the comparison of elements within the container, so the elements should support relational operations.
Container size operation:
C.size ()//Current size
C.max_size ()//accommodates the maximum number of
C.empty ()
C.resize (n)//resize to N, if N>c.size (), Add value takes initialization method, otherwise delete extra value
C.resize (n,t)//resize to N, if N>c.size (), add value to t, otherwise delete extra value
Element access:
If the container is not empty, Then the front and back members return the first or last element reference within the container:
if (!ilist.empty ())
{
list<int>::reference val=* Ilist.begin ();//reference
list<int>::reference Val2=ilist.front ();//and Val same
List<int>::reference Last=*--ilist.end ();
list<int>::reference last2=ilist.back ();
}
Rollup:
C.back ()
C.front ()
c[n] //only vector and deque suported, No list
c.at (n) //only vector and deque suported, no list
Delete element:
c.erase ( p) |
Delete the element pointed to by the iterator, returning an iterator to the next element of the deleted element, if P is c.end () The function is undefined |
c.erase (b,e) |
deletes the elements in the iterator B and E ranges, returns an iterator to the next element of the deleted element segment, and returns C.end () |
c.clear () |
Delete all elements in C, return void |
c.pop_back () |
Deletes the last element, returns void, and if C is null, the function does not define |
c.pop_front () /td> |
deletes the first element, returns void, and if C is null, the function is undefined and applies only to list or deque |
String Searchvalue ("Quasimodo");
List<string>::iterator Iter=find (Slist.begin (), Slist.end (), searchvalue);
if (Iter!=slist.end ())
slist.erase (ITER);
Assign and swap
C1=c2 |
Delete all the contents of C1 and copy the C2 elements to C1. C1 and C2 must be of the same type |
C1.swap (C2) |
Exchange content: Type to be the same. The execution speed is usually faster than copying the C2 element to the C1 |
C.assign (B,e) |
Resets the elements of C: Copies the elements in the range of the iterator B and E tags to c. B and E cannot point to elements in C |
C.assign (n,t) |
Reset C to store n elements with a value of t |
when in different or identical containers, the element types are not the same but are compatible with each other, they cannot be assigned by = and are assigned with assign.
Swap: The container type is the same and the element type is the same.
C1.swap (C2);
the swap operation does not move the element, so the iterator is not invalidated. For example, before swapping, an iterator points to c1[2], after the interchange is implemented, the iterator points to c2[2], pointing to the same object.
- Container Self-growth
In order for the vector container to achieve fast memory allocation, the vector container reserves additional storage to hold the newly added elements.
since vectors are stored continuously in memory, the vector container handles the details of memory allocations as part of its implementation, which is held by the interface of the vector. The vector class provides two member functions: Capacity and reserve, allowing the programmer to interact with the implementation part of the vector capacity memory allocation. The capacity operation gets the total number of elements that can be stored before the container needs to allocate more storage space, while the reserve operation tells the vector container how many elements the storage space should be reserved.
size refers to the total number of tuples currently owned by the container, while capacity is the total number of elements that the container can store before it must allocate new storage space.
vector<int> Ivec;
cout<< "ivec:size:" <<ivec.size () <<endl; 0
cout<< "ivec:capacity:" <<ivec.capacity () <<endl; 0
//After adding 24 elements
size:24
capacity:32
Ivec.reserve (+);
size:24
capacity:64
//After adding 24 elements
size:48
capacity:64//use of reserved capacity, no space allocated
//Add 24 more elements
Size:7
capacity:128//Reallocate storage space to double the allocation of current capacity (varies by compiler)
- Container Selection
|
Vector |
Deque |
List |
Random Access |
Fast |
Fast |
Not supported |
Insert/erase |
Low efficiency |
Lowest efficiency |
Fast |
Push_front () |
Not supported |
Fast |
Fast |
Push_back () |
Fast |
Fast |
Fast |
Pop_front () |
Not supported |
Fast |
Fast |
Pop_back () |
Fast |
Fast |
Fast |
List does not support random element access:
ilist.at (0);//error
Ilist[0];//error
Vector does not support Push_front (Val) and Pop_front () operations.
tips for selecting containers:
A. If your program requires random access to elements, you should use a vector or deque container.
B. The list container should be used if the program must insert or delete elements in an intermediate location.
C. If you do not need to insert or delete elements in the middle, but instead insert or delete elements at the beginning or end, you should use the Deque container.
D. If you simply insert an element in the middle of the container while reading the input and then need to randomly access the element, consider reading the element into a list container at input, then reordering the container so that it is suitable for sequential access and then copying the sorted list container into the vector.
E. If the program requires random access and must insert or delete elements in the middle of the container, the choice of which container depends on the cost of the following two operations: the cost of randomly accessing the list, the cost of copying elements when inserting/deleting elements in a vector or deque container.
- String and Container
The string type supports most sequential operations and, in some respects, treats the string type as a character container. Unlike a vector container, it does not support the manipulation of containers on a stack: Front,back and pop_back operations cannot be used in string types.
A container operation that is not supported by string:
C<t> c (N) initialization
Push_front (e)
Back,front
Pop_back () Pop_front ()
For a specific introduction to string, see the article "about string in the C + + standard library"
- Container Adapter
In addition to the sequential container, the standard library provides three sequential container adapters: Queue,priority_queue and Stack.
common operations supported by the adapter are:
Size_type,value_type, Container_type, A; A (c); Relational operators
#include <stack>
#include <queue>
Initialize:
two ways: default constructor, empty object; A constructor with a container parameter takes a copy of the parameter container as its underlying value.
Example: If DEQ is a container of type deque<int>, a new stack:stack<int> Stk (DEQ) can be initialized using DEQ;
overriding the underlying container type:
the default stack and queue are based on the Deque container, while the priority_queue is implemented on the vector container. When you create an adapter, you can override its associated underlying container type by specifying a sequential container as the second type parameter of the adapter:
stack< string, vector<string> > Str_stk;//implemented on top of Vector
stack<string, vector<string> > Str_stk (SVEC);//holds a copy of Svec
Stack Adapter The container to be associated with can be any one of the sequential containers, so it can be built on vector,list or DEUQE.
The queue adapter requires that its associated underlying container must provide a push_front operation, so it can only be built on a list container and not on a vector.
The priority_queue adapter requires random access, so it can be built on a vector or deque container and cannot be built on a list container.
Stack Adapter:
s.empty (), S.size (), S.pop (), S.top (), S.push (item)
queue: FIFO queue
q.empty (), Q.size (), Q.pop (), Q.front (), Q.back (), Q.push (item)
C + + sequential container