Reference manual for vector in STL of standard template library

Source: Internet
Author: User

Introduction:

Vector is considered a container because it can store various types of objects like a container. Simply put, vector is a dynamic array that can store any type, data can be added and compressed.

To use vector, you must include the following code in your header file:

# Include <vector>Note: The header file does not contain ". H"

Vector belongs to the STD naming field. Therefore, you need to complete the Code as follows:

Using STD: vector;

Vector <int> vints;

Or, use the full name:

STD: vector <int> vints;

We recommend that you use the global namefield method when the amount of code is small and the namespace used is small:

Using namespace STD;

Structure:

This constructor also has an optional parameter, which is an instance of the T type and describes the initial values of each member of each vector;

For example: vector <int> V2 (init_size, 0); if it is pre-defined: int init_size; its member values are initialized to 0;

Copy the constructor to construct a new vector and use it as the complete copy of an existing vector;

For example, vector <int> V3 (V2 );

A constructor with two constant parameters generates a vector with an initial value as an interval. The interval is specified by a semi-open interval [first, last.

For example, vector <int> V4 (first, last)

 

#include <vector>        //The default vector constructor takes no arguments, creates a new instance of that vector.    vector();      //The second constructor is a default copy constructor that can be used to create   //a new vector that is a copy of the given vector c.   vector( const vector& c );      //The third constructor creates a vector with space for num objects. val is specified,   //each of those objects will be given that value. For example, the following code creates   //a vector consisting of five copies of the integer 42:vector<int> v1( 5, 42 );   vector( size_type num, const TYPE& val = TYPE() );      //The last constructor creates a vector that is initialized to contain the elements  end   vector( input_iterator start, input_iterator end );      ~vector();    

 

Member functions:

   
   
Assign Assign elements to a vector
At Returns an element at a specific location
Back Returns a reference to last element of a vector
Begin Returns an iterator to the beginning of the Vector
Capacity Returns the number of elements that the vector can hold
Clear Removes all elements from the Vector
Empty True if the vector has no elements
End Returns an iterator just past the last element of a vector
Erase Removes elements from a vector
Front Returns a reference to the first element of a vector
Insert Inserts elements into the vector
Max_size Returns the maximum number of elements that the vector can hold
Pop_back Removes the last element of a vector
Push_back Add an element to the end of the Vector
Rbegin Returns a reverse_iterator to the end of the Vector
Rend Returns a reverse_iterator to the beginning of the Vector
Reserve Sets the minimum capacity of the Vector
Resize Change the size of the Vector
Size Returns the number of items in the vector
Swap Swap the contents of this vector with another

 

The Chinese table is:

C. Assign (beg, end)

Assign the value of data in the [beg; End) interval to C.

C. Assign (n, ELEM)

Assign a copy of n elem to C.

C. At (idx)

Returns the data specified by the index idx. If idx is out of bounds, out_of_range is thrown.

C. Back ()

Returns the last data and does not check whether the data exists.

C. Begin ()

Returns the first data address in the iterator.

C. Size ()

Returns the actual number of data in the container.

C. End ()

Points to the next end element in the iterator, pointing to a nonexistent Element

C. Clear ()

Remove all data from the container

C. Empty ()

Judge whether the container is empty

C. Erase (POS)

Deletes the data at the POs position and returns the location of the next data.

C. Erase (beg, end)

Delete the data in the [beg, end) interval and return the location of the next data.

C. Front ()

Returns the first data.

C. insert (Pos, ELEM)

Insert an ELEM copy at the POs position and return the new data location.

C. insert (Pos, N, ELEM)

Insert n ELEM data at the POs position. No return value

C. insert (Pos, beg, end)

Data inserted in the [beg, end) range at the POs position. No return value

C. max_size ()

Maximum number of returned data in the container

C. pop_back ()

Delete the last data

C. push_back (ELEM)

Add data at the end

C. rbegin ()

Returns the first data of a reverse queue.

C. rend ()

Returns the next location of the last data in a reverse queue.

C. Resize (Num)

Specify the queue length again

C. Reserve ()

Reserve proper capacity

C1.swap (C2)

Swap C1 and C2 Elements

Swap (C1, C2)

Swap C1 and C2 Elements

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Stack implementation using containers:

# Ifndef stack # define stack # include <vector> template <class T, int capacity = 30> class Stack {public: Stack () // constructor {pool. reserve (capacity); // set the minimum container capacity} void clear () // clear the stack {pool. clear (); // clear the container} bool isempty () const // determine whether the stack is empty {return pool. empty (); // whether the container is empty} T & Topel () // gets the top element of the stack {return pool. back (); // get the last element of the container} t POP () // output stack {T El = pool. back (); // retrieves the last element pool of the container. pop_back (); // Delete the last element return El;} void push (const T & El) // stack into {pool. push_back (EL); // Add an element at the end of the container} PRIVATE: vector <t> pool ;}; # endif

 

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.