Exploring the vector of STL containers

Source: Internet
Author: User
Vector

 

Features:

 

1. Dynamic Array with Variable Length
2. Include the header file # include <vector>
3. Support Random Access iterator
? The time for Random Access to an element based on the subscript is constant.
? Fast addition at the end
? Slow insert in the middle

 

 

Member Functions

 

Initialization

 

Vector (); initialized to empty vector (int n); initialized to have n elements vector (int n, const T & Val); initialized to have n elements, the value of each element is Val and the type is tvector (iterator first, iterator last). The value is initialized to the content consistent with the [first, last) range on other containers.


Example:

1. Create a Null vector object of the int type:

Vector <int> intvec;

2. Create a vector containing five int data types:

Vector <int> intvec (5 );

3. Create a vector containing five int data types and initialize them to 2:

Vector <int> intvec (5, INT (2 ));

4. Create by copying a vec container

Vector <int> intvec (VEC. Begin + M, VEC. Begin + n)

 

 

 

 

Element access

 

 

Intvec. At (INDEX)

Returns the element at the position specified by the index.

Intvec [Index]

Returns the element at the position specified by the index.

 

 

 

 


Example:

 

include <iostream>#include <vector>using namespace std;int main(){    vector<int> vec(10);    for (int i = 0;i != vec.size();i++)    {        vec.at(i) = i;        //vec[i] = i ;    }    for (int i = 0;i != vec.size();i++)    {        cout << vec.at(i)<<endl;        //cout <<vec[i]<<endl;    }             return 0;}


 

 

 

 

Container size

 

Expression

Function

Intvec. Capacity ()

Returns the maximum number of elements that can be inserted into the intvec container.

Intvec. Empty ()

If the container intvec is empty, true is returned. Otherwise, false is returned.

Intvec. Size ()

Returns the current number of intvec containers.

Intvec. Resize (Num)

Change the number of elements to num. If size () is increased, the default constructor is responsible for creating these new elements.

Intvec. Resize (Num, ELEM)

Change the number of elements to num. If size () increases, the default constructor initializes these new elements to ELEM

 

 

Example:

 

# Include <iostream> # include <vector> using namespace STD; int main () {vector <int> vec1; cout <vec1.empty () <Endl; // returns 1 cout <vec1.size () <Endl; // returns 0 vector <int> vec2 (20); cout <vec2.empty () <Endl; // return 0 cout <vec2.size () <Endl; // returns 20 vector <int> vec3 (); cout <vec3.capacity () <Endl; // return 15 vec3.push _ back (1); cout <vec3.size () <Endl; // return 16 cout <vec3.capacity () <Endl; // return 30 // If the array is not enough, the next re-allocation will be doubled to return 0 ;}


 

Container operations

 

Statement

Function

Intvec. Clear ()

Delete all elements from the container

Intvec. Erase (position)

Deletes an element at a position specified by position.

Intvec. Erase (beg, end)

Delete all elements from beg to end-1

Intvec. insert (Position, ELEM)

Insert a copy of ELEM to the position specified by position and return the position of the new element.

Intvec. inser (Position, N, ELEM)

Insert n copies of ELEM to the position specified by position.

Intvec. insert (Position, beg, end)

Insert copies of all elements from beg to end-1 to the position specified by position in intvec.

Intvec. push_back (ELEM)

Insert a copy of ELEM to the end of the vector.

Intvec. pop_back ()

Delete last element

Intvec. Front ()

Returns the reference of the first element without checking whether the container is empty.

Intvec. Back ()

Returns the reference of the last element without checking whether the container is empty.

 

 

 

# Include <iostream> # include <vector> using namespace STD; int main () {vector <int> VEC (10, 1); cout <Vec. empty () <Endl; // returns 0 Vec. clear (); cout <Vec. empty () <Endl; // returns 1 vector <int> vec1 (); vec1.insert (vec1.begin () +); // Insert at this position, move the original position behind the entire element after it for (INT I = 0; I! = Vec1.size (); I ++) cout <vec1.at (I); // 12 cout <Endl; vec1.push _ back (3); For (INT I = 0; i! = Vec1.size (); I ++) cout <vec1.at (I); // 123 cout <Endl; vec1.pop _ back (); vec1.pop _ back (); for (INT I = 0; I! = Vec1.size (); I ++) cout <vec1.at (I); // 1 cout <Endl; return 0 ;}


 

 

# Include <iostream> # include <vector> using namespace STD; int main () {vector <int> VEC (10, 1); Vec. push_back (2); // 12 cout <Vec. front () <Endl; // returns 1 cout <Vec. back () <Endl; // returns 2 Int & head = Vec. front (); Head ++; Int & tail = Vec. back (); tail --; cout <Vec. front () <Endl; // returns 2 cout <Vec. back () <Endl; // return 1 return 0 ;}


 

Two-dimensional array

 

Vector <vector <int> V (3 );
// V has three elements,
// Each element is a vector <int> container.

 

#include <iostream>#include <vector>using namespace std;int main() {    vector< vector<int> > v(3);    for(int i=0; i<v.size(); ++i)        for(int j=0; j<4; ++j)            v[i].push_back(j);    for(int i=0; i<v.size(); ++i){        for(int j=0; j<v[i].size(); ++j)            cout<<v[i][j]<<" ";        cout<<endl;    }    return 0;}


Output:

0 1 2 3

0 1 2 3

0 1 2 3

 

 

 


 

 

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.