We all know that STL containers divide containers into two types: sequential containers and associated containers.
Ordered containers include vector, deque, and list.
I. Common Operations of sequential containers
1: first, the iterator of the sequential container
Definition: T <C >:: iterator ITER;
/* Supports all sequential containers */
* ITER returns the iterator reference.
ITER-> mem refers to ITER, which is equivalent to (* ITER). Men
++ ITER | ITER ++ auto-Addition
-- ITER | ITER -- auto-Subtraction
Iter1 = iter2 | iter1! = Iter2 comparison
/* Only supports vector and deque */
ITER + N | ITER-N iterator addition and subtraction integer
Iter1 + = iter2 | iter1-= iter2 iterator compound operation
Subtraction of iter1-iter2 iterator
>,>=,<<= Iterator link Operator !! It can also be used to compare the sizes between iterators.
2: iterator range
C. Begin () | C. rend () indicates the first element in the container, followed by the reverse iterator.
C. End () | C. rbegin () indicates the last element of the container.
C. Size () returns the number of container elements
C. max_size () returns the maximum number of elements allowed
C. Whether empty () is null. A boolean value is returned.
C. Resize (n)Adjust the container length to accommodate n elements.
C. Resize (n, T) contains n elements. Newly Added elements are T.
C. Capacity () gets the total number of elements that can be stored in the current container.
C. How much space is reserved by the reverse (n) for the container?
3: Type alias defined by the container
Size_type unsigned integer, container Length
Iterator type
Const_iterator read-only iterator type
Reverse_iterator backward addressing iterator
Const_reverse_iterator read-only backward addressing iterator
Difference_type iterator difference value, which can be negative
Value_type element type
Reference element left Value Type
Const_reference constant element left Value Type
/* Definition method C <t>: Type alias variable;
4: Container Initialization
C <t> C; create an empty container
C <t> C (C2); Create a copy of the container from C2
C <t> C (B, E); Create a container as a copy of the iterator B to E
C <t> C (n, T); creation is initiated by N telements
C <t> C (n); creates n initialized containers.
5. add and delete Elements
C. add elements at the end of push_back ()
C. adding elements to the push_front () header is not applicable to Vector
C. insert (P, T) iterator P position add t
C. insert (p, N, T) iterator P position add n t
C. insert (p, B, E) The P position of the iterator is updated to an element from B to E.
Delete Element
C. Erase (p) deletes the element at position P of the iterator.
C. Erase (B, E) deletes the elements in the range from B to E.
C. Clear () Clear all elements
C. pop_back () Delete the last element of the container
C. pop_front () deletes the first element, which is not applicable to Vector
6. Access container elements
C. Back () returns the reference of the last element in the container.
C. Front () returns the first element reference
C [I] | C. at (I) returns the I-th element reference, not applicable to list
7. Assignment and swap
C1 = c2c1 assign a value of C2. Delete the elements in C1 and copy C2 to C1.
C1.swap (C2) Swap elements in C1 and C2
C. Assign (B, E) uses the Element B to E of the iterator to update all c
C. Assign (n, T) C is updated to n t
Ii. Differences between ordered containers and selection of containers
1: vector and deque can access random elements. Vector and deque are overloaded with the operator "[]", but the list is not. We can only access it through the iterator, but cannot be accessed through subscript.
2: If you insert more elements in the middle position, select list first. If you insert more elements in the header and tail, select deque first.
3: in programming, containers can interwork to improve efficiency. If you only need to insert elements in the middle during reading, and you need to randomly access elements, we can use list to read the elements, then copy to the vector.
Note: we know that the vector is continuously allocated memory. If the memory is full after the new element is added, the vector must find a larger internal training and copy the current element, then, release the previous memory. If this operation is performed multiple times, it will be a waste of time. The solution is to define that the container allocates space and more reserved space, for a long time, there will be no replication of all elements.
Exercise code:
# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> # include <vector> # include <queue> # include <list> using namespace STD; # define del (a, B) memset (a, B, sizeof (A) const int n = 11000; int main () {/* 9.1.1 */vector <int> Ievel; vector <string> SVEC; // (get_word_count ("Chimera"); SVEC. push_back ("I CCC"); SVEC. push_back ("Yang can"); SVEC. insert (SVEC. begin () + 1, "momo"); SVE C. insert (SVEC. begin () + 1, 5, "hahha"); string s [10] = {"what", "is", "your", "name"}; SVEC. insert (SVEC. begin () + 1, S, S + 4); vector <string >:: iterator I = SVEC. begin (); SVEC. erase (I + 0); // SVEC. erase (I, I + 2); For (INT I = 0; I <SVEC. size (); I ++) cout <SVEC [I] <Endl; cout <"---->" <SVEC [10] <Endl; cout <"XXXXX" <SVEC. at (10) <Endl; // another access method. The subscript overthrows out_of_range/* vector <string >:: const_reverse_iterator II; For (II = SVEC. rbegin (); II! = SVEC. rend (); II --) {cout <* II <Endl;} * // * List <string> slist (SVEC. begin (), SVEC. end (); List <string >:: iterator it; for (IT = slist. begin (); it! = Slist. end (); It ++) cout <* It <Endl; const list <int >:: size_type LL = 64; // note the type list <string> ls (LL, "eh? "); For (IT = ls. Begin (); it! = Ls. end (); It ++) {}// cout <* It <Endl;/* 9.1.2 vector <string> V; vector <string> S1; v. push_back (S1); */list <int> Lis (10, 42); Lis. resize (15); // adjust the container size Lis. resize (25,-1); // optional parameter Initial Value/LIS. resize (5); List <int >:: iterator it = Lis. begin (); For (; it! = Lis. End (); It ++) {cout <* It <Endl;} return 0 ;}