The so-called container, is a box of things, in C + +, we put things called "element"
and sequential containers, which means that these things are in order, what order you put in, what order they are inside.
There are several sequential containers in C + +:
Vector |
Variable size array |
Deque |
Dual-ended queues |
List |
Doubly linked list |
Forward_list |
One-way linked list |
Array |
Fixed array size |
String |
A container similar to a vector, but designed to hold characters |
These containers allow us to easily put elements, take elements, but they are in: Add, delete, non-sequential access, these aspects of performance are different drops (as appropriate to choose)
List A lot of, is to have an impression, do not know what they do, the back slowly learn ...
First of all, let's take a look at common things, and here's what all container types do.
Type aliases |
|
Iterator |
Iterators |
Const_iterator |
Read-only iterator |
Size_type |
unsigned int, which guarantees the maximum possible length of the container can be stored |
Difference_type |
int, enough to hold the distance between the two iterators, is a signed |
Value_type |
Element type |
Reference |
The left value type of the element, that is, value_type& |
Const_reference |
That is, the const Value_type & |
constructor function |
|
C C |
Default constructors, constructing empty containers |
C C1 (C2) |
Constructs the C1, the content is copies C2 's |
c C (b, E) |
Constructs C, copies the elements in the range specified by Iterators B and E to C |
C C{a, B, c ...} |
List Initialization C |
Assignment and Swap |
|
C1 = C2 |
Replace an element in C1 with an element in C2 |
C1 = {A,b,c ...} |
Replace elements in C1 with elements from a list (array not applicable) |
A.swap (b) |
Swap the elements of a A, b |
Size |
|
C.size () |
Number of elements in C (forward_list not applicable) |
C.max_size () |
c The maximum number of elements that can be saved |
C.empty () |
c is empty, null true, not empty false |
Add/Remove elements |
|
C.insert (args) |
Copy the elements in args into C |
C.emplace (inits) |
Using inits to construct an element in C |
C.erase (args) |
Delete the element specified by args |
C.clear () |
Delete all elements in C, return void |
Relational operators |
|
==, != |
Are they equal, or are they unequal? |
<, <=; >= |
Relational operators |
Get iterators |
|
C.begin (), C.end () |
The iterator that gets the first element, the position after the trailing element, |
C.cbegin (), C.cend () |
Back to Const_iterator |
Additional members of the reverse container |
|
Reverse_iterator |
An iterator that is a reverse address |
Const_reverse_interator |
Read-only reverse iterator |
C.rbegin (), C.rend () |
tail element, iterator before the first element |
C.crbegin (), C.crend () |
Back to Const_reverse_iterator |
Better... I'm all knocked out.
First we have eggs, we see "constructors", which are divided into the following categories:
(1) Default initialization, (2) Copy initialization, (3) list initialization, (4) iterator initialization, (5) sequential container-specific initialization
Make a chestnut with a vector, the code is as follows:
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 intMain ()7 {8vector<int> vec1;//Default Initialization9vector<int> vec2{1,2,3,4};//Initialization of listsTenvector<int> vec3{5,6,7,8}; OneVEC3 =vec2; Avector<int> Vec4 (VEC3);//Copy Initialization -vector<int> Vec5 (vec4.begin () +1, Vec4.end ()-1);//Iterator Initialization -vector<int> Vec6 (Ten,2);//sequential container-specific initialization the return 0; -}
seq_container_definition
Execution results gdb look at:
OK, then let's look at the iterator, and begin () and end ()
Iterators and pointers are like, anyway, let's go through the access element, begin () take the first element, end () fetch? Trailing element (one past the last element)
Here's a snippet to see her use:
1#include <iostream>2#include <string>3 4 using namespacestd;5 6 intMain ()7 {8 stringS"Time for lunch");9 string:: Iterator iter =S.begin ();Ten while(ITER! =S.end ()) { One*iter = ToUpper (*iter); Aiter++; - } -cout << S <<Endl; the return 0; -}
Seq_container_iterator
Note that if you add or remove elements to a container object, the object's capacity is changed, and the original iterator on the object is invalidated.
C + + sequential container learning