In-depth analysis of C ++ Vector usage
Source: http://developer.51cto.com/art/201002/183645.htm
(1) vector <type> identifier;
(2) vector <type> identifier (maximum capacity );
(3) vector <type> identifier (maximum capacity, initial values );
(4) int I [4] = {12, 3, 4, 5 };
| 1 |
Vector <type> vi (I, I + 2); // obtain the value after I index value is 3; |
(5) vector <int> // vi defines a 2-dimensional container. remember to have spaces. Otherwise, an error will be reported.
| 123456789 |
Vector Line // when using it, you must first initialize the vi rows; for (inti = 0; I <10; I ++) {vector. push_back (line) ;}/// I personally think it is good to define a two-dimensional array using vector, because the length can be unknown. Good. |
(6) C ++ Vector sorting
| 123456 |
Vector Vi; vi. push_back (1); vi. push_back (3); vi. push_back (0); sort (vi. begin (), vi. end (); // small to large reverse (vi. begin (), vi. end () // small from Avenue |
(7) sequential access
| 1234567891011121314 |
Vector Vi; for (inti = 0; I <10; I ++) {vector. push_back (I) ;}for (inti = 0; I <10; I ++) // The first call method {cout < : Iterator it = vi. begin (); it! = Vi. end (); it ++) // The second call method {cout <* it <"";} |
(8) search
| 1234567 |
Vector Vi; for (inti = 0; I <10; I ++) {vector. push_back (I);} vector : Interator it = find (vi. begin (), vi. end, 3); cout <* it <endl; // return the position of the value in the container. |
(9) use arrays to initialize the C ++ Vector.
| 12345678 |
Inti [10] = {1, 2, 4, 5, 6, 7, 78, 8}; // The first vector Vi (I + 1, I + 3); // From 2nd elements to the third element for (vector : Interator it = vi. begin (); it! = Vi. end (); it ++) {cout <* it <"";} |
(10) struct type
| 1234567891011121314151617 |
Structtemp {public: string str; public: intid;} tmpintmain () {vector T; temp w1; w1.str = "Hellowor"; w1.id = 1; t. push_back (t1); cout <w1.str <"," < |
Vector container member functions
| 123456789101112131415161718192021 |
Assign () // assign an at () value to the element in the container, return the back () element at the specified position, return the last element begn (), and return the iterator capacity () of the first element () returns the number of elements that a vector can hold, clear (), clear all elements, empty (), and determine whether the vector is empty. returns the last iterator erase (); deletes the specified element front (); returns the first element get_allocator (); returns the vector memory distributor insert (); and inserts it into the vector container. max_size (); returns the maximum number of pop_back () of the vector; removes the last element push_back (); adds the element rbegin () to the vector; returns the iterator at the end of the vector. rend (); returns the starting iterator reserve (); sets the minimum number of elements in the vector to resize (); changes the size of the number of elements in the vector (); return the size of the number of elements swap (); swap two vectors; |