In-depth analysis of C ++ Vector usage

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.