C + + Learning: vector usage

Source: Internet
Author: User

Function

Function prototypes

Description

constructor function

Vector ();

Create an empty vector

Vector (int nSize);

Create a vector with the number of elements nsize

Vector (int nsize,const t& t);

Creates a vector with a number of elements of nsize and a value of T

Vector (const vector&);

Copy constructor

Vector (begin,end);

Copy the elements of another array within the [Begin,end] interval into the vector

Property

BOOL empty () const;

Determines whether the vector is empty, and if it is empty, no element in the vector

int size () const;

Returns the number of elements in a vector

int capacity () const;

Returns the maximum element value that the current vector can hold

int max_size () const;

Returns the maximum allowable vector element number value

accessing elements

[]

Reference at (int pos);

Returns a reference to the POS location element

Reference Front ();

Returns a reference to the first element

Reference back ();

Returns a reference to the tail element

Insert

void push_back (const t& x);

Add an element x to the tail of the vector

Iterator Insert (iterator it,const t& x);

Insert an element x before the element pointed to by the iterator

Iterator Insert (iterator it,int n,const t& x);

An iterator that points to an element before inserting n identical elements x

Iterator Insert (iterator it,const_iterator first,const_iterator last);

Insert data between [first,last] of another vector of the same type before the element pointed to by the iterator

Delete

Iterator Erase (iterator it);

Delete the element that the iterator points to in the vector

Iterator Erase (iterator first,iterator last);

Delete an element between [first,last] in a vector

void Pop_back ();

Delete the last element in a vector

void Clear ();

Empties all elements in the vector

Iterators

Iterator begin ();

Returns the vector head pointer, pointing to the first element

Iterator End ();

Returns the vector tail pointer, pointing to the next position of the last element of the vector

Reverse_iterator Rbegin ();

Reverse iterator, pointing to the last element

Reverse_iterator rend ();

A reverse iterator that points to the position before the first element

Exchange

void swap (vector&);

Exchange data for two vectors of the same type

Assign value

void assign (int n,const t& x);

Sets the value of the nth element in a vector to X

void Assign (Const_iterator first,const_iterator last);

The element in the vector [First,last] is set to the current vector element

Algorithm

Reverse (Vec.begin (), Vec.end ());

Flip an element

Sort (Vec.begin (), Vec.end ());

The default is in ascending order, that is, from small to large. You can use the Override sort comparison function to compare in descending order: Define the sort comparison function:

BOOL Comp (const int &a,const int &b) {

Return a>b;

}

Call: Sort (Vec.begin (), Vec.end (), Comp)


C + + Learning: vector usage

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.