Vector API Detailed Learning

Source: Internet
Author: User

Vector is a sequential container .

constructor function:

  vector ();   Const vector& c);   Const type& val = TYPE ());  Vector (input_iterator start, Input_iterator end);

Example:

#include <iostream>#include<vector>using namespacestd;intMain () {//First type: no parametersvector<int>v; V.push_back (Ten); V.push_back (1);  for(auto i:v) cout<< I <<Endl; //The second type: Initialize with existing vectorsvector<int> v2 (v);//copy a container to another container, both the container type and the element type must be the same     for(auto i:v2) cout<< I <<Endl; //The third type: Specify the number of elements in the container, with the default value for each elementvector<int> V3 (4);//the default value for int is 0     for(auto i:v3) cout<< I <<Endl; //Fourth: Specify the number of elements within the container, specifying the value of each elementvector<int> v4 (4,1);//4 x 1     for(auto i:v4) cout<< I <<Endl; //Fifth: Assigning elements in the iterator's specified range to the containerAuto Beginit =V.begin (); Auto Endit=V.end (); Vector<int>V5 (Beginit, Endit);  for(auto i:v5) cout<< I <<Endl; return 0;}

Note: (1) Copy a container to another container, both the container type and the element type must be the same.

(2) The pointer is also an iterator.

    int a[4] = {1,2,3,4};    Vector<int> V5 (A, A +4)    ;  for (auto i:v5)         << i << Endl;
View Code

(3) Other container iterators can also be used to initialize the vector

list<int> l;    L.push_back (ten);    L.push_back (4);    Vector<int> V6 (L.begin (), L.end ());      for (auto i:v6)         << i << Endl;
View Code

Vector Common API:

Now analysis:

1. Assign

This is an assignment method, but overwrites the value in the original container.

  void Const type& val); Num a Val value  of void Assign (input_iterator start, Input_iterator end);

Example:

//First: Assign a value of num valvector<int> V (3,5);//this is initialized to 3 of 5V.assign (4,Ten);//will overwrite the above initial value     for(auto i:v) cout<< i << Endl;//output 4 x 0//The second type: Using Iteratorsvector<int>v2 (V.begin (), V.end ());  for(auto i:v) cout<< i << Endl;//output 4 x 0

2. At

Returns the value of the corresponding coordinate, which is accessed using the subscript.

3. Back

Returns the value of the last element.

    vector<int> v;    V.push_back (1);    V.push_back (2);     // Output 2

4. Begin

Returns an iterator that points to the first element.

5. Capacity

Returns the capacity of the vector container, the note and size are different, the capacity is always greater than or equal to the size. Vectors always define a larger value as the capacity, so that you do not have to switch to a larger space each time you increment the element (which takes time). When the element is increased to more than capacity, capacity automatically doubles.

    vector<int> v;    V.push_back (1);    V.push_back (2);    V.push_back (3/ / Total 3 elements    // output 4

6. Clear

Clears all elements, becomes empty (not the default), but capacity remains unchanged.

vector<int> V (1); V.clear ();

7. Empty

Determines whether the vector is empty.

Vector<int> v;    V.push_back (1);    V.push_back (2);    V.push_back (3);    V.clear ();    cout << v.empty () << Endl; Output 1

  

8. End

Returns an iterator that points to the back of the last element.

9. Erase

Removes the specified element from the iterator.

  Iterator Erase (iterator loc);  Iterator Erase (iterator start, iterator end);

Example:

    //First: Remove the element specified by the iteratorvector<int>v; V.push_back (1); V.push_back (2);    V.erase (V.begin ()); cout<< v[0] << Endl;//Output 2//Second Type: Remove the range specified by the iteratorV.push_back (1); V.push_back (2); V.push_back (3); V.erase (V.begin (), V.begin ()+2);  for(auto i:v) cout<< i << Endl;//Output 3

PS. Time-consuming O (n)

Ten. Front

Returns a reference to the first element.

One. Insert

Inserts an element before the specified position.

Method:

  Const type& val);   void Const type& val);  Templatevoid Insert (iterator loc, input_iterator start, Input_iterator end);

Example:

vector<int> V (2,1); Vector<int> Temp (3,3); V.insert (V.begin (), One);//Insert 11 before begin, that is, 11 becomes the first elementV.insert (V.begin (),2, A);//Insert 2 x before beginV.insert (V.begin (), Temp.begin (), Temp.end ());//Insert all elements of temp from begin to end before begin     for(auto e:v) cout<< e << Endl;

Max_size.

Returns the upper limit of the number of elements the vector can hold. Note that the size is the current number of elements, capacity is slightly larger than the size of the capacity, and max_size is the maximum number that can be accommodated.

On my 64-bit machine, the value is 4611686018427387903, or 8 bytes.

Pop_back.

Removes the last element.

Push_back.

Add elements to the last.

    vector<int> v;      for (int i=0; i<; i++)        v.push_back (i);      for (auto e:v)         << e << Endl;

Rbegin.

Returns the Reverse_iterator (inverse iterator), pointing to the last element, and the direction is moving toward begin (the normal iterator plus positive value moves toward the tail of the vector).

  

Rend.

Returns a pointer to the reverse_iterator preceding the first element.

    vector<int> v;      for (int i=0; i<; i++)        v.push_back (i);     = V.rbegin ();      while (I! = v.rend ()        ) << *i++ << Endl; Reverse output vector

. Reserve

Sets the size of the capacity, which is not valid if it is smaller than the original capacity.

vector<int> V (Ten,1); V.reserve ( the); cout<<"Capacity:"<< v.capacity () << Endl;//OutputV.reserve (5); cout<<"Capacity:"<< v.capacity () << Endl;//Output     for(auto e:v) cout<< e <<" ";

Resize.

Method:

void Const type& val = TYPE ());

Example:

    //First Kindvector<int> V (Ten,1); V.resize ( the); cout<< v.size () << Endl;//Output     for(auto e:v) cout<< e <<" ";//5 more elements to fill with the default value 0cout <<Endl; V.resize (5); cout<< v.size () << Endl;//Output 5     for(auto e:v) cout<< e <<" ";//The extra elements have been removed//The second KindV.resize ( the,8);//specifies that an extra-out element is assigned a value of 8cout << v.size () << Endl;//Output     for(auto e:v) cout<< e <<" ";//the extra element is populated with a specified value of 8

. Size

Returns the number of elements currently contained.

. Swap

Swaps the contents of the specified container.

Method:

void  from);

Example:

    vector<int> v1 (4,1);    Vector<int> V2 (6,4);    V1.swap (v2);      for (auto e:v1)         << e << Endl;      for (auto e:v2)         << e << Endl;

Vector API Detailed Learning

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.