Simple application of C++--vector

Source: Internet
Author: User

In C + +, vector is a very useful container, and here is a summary of the container.

1 Basic operations

(1) header file #include<vector>.

(2) Create vector object,vector<int> VEC;

(3) Insert number at tail: vec.push_back (a);

(4) Use subscript to access the element,cout<<vec[0]<<endl; remember that the subscript is starting from 0.

(5) Use iterators to access elements.

vector<int>:: iterator it;  for (It=vec.begin (); It!=vec.end (); it++)    cout<<*it<<endl;

(6) Insert element: Vec.insert (Vec.begin () +i,a); Insert a in front of the first I+1 element;

(7) Delete element: Vec.erase (Vec.begin () +2); Delete 3rd element

Vec.erase (Vec.begin () +i,vec.end () +j); Delete interval [i,j-1]; interval starting from 0

(8) Vector size: vec.size ();

(9) Empty: Vec.clear ();

2. Here are some of the uses of my own tests, for reference only:

#include <iostream>#include<vector>#include<stdio.h>using namespaceStd;typedefstructa{intID; intlength; intwidth; //for some of the elements of the vector is a struct, you can define the comparison function in the structure, the following sort in ascending order of Id,length,width    BOOL operator< (ConstA &a)Const{        if(id!=a.id)returnid<a.id; Else{            if(length!=a.length)returnlength<a.length; Else                returnwidth<A.width; }}}a;intmain () {vector<int>VI; Vi.push_back ( A);//This is the element that is inserted from the end of the vectorVi.push_back ( -); Vi.pop_back ();//remove an element from the endVi.push_back ( +); Vector<int>::iterator ITER;//Iterating through an element in a vector through an iterator      for(Iter=vi.begin (); Iter!=vi.end (); iter++) {cout<<*iter<<" "; } cout<<Endl; Vi.insert (Vi.begin ()+1,2);//inserts an element at the specified location       for(Iter=vi.begin (); Iter!=vi.end (); iter++) {cout<<*iter<<" "; } cout<<Endl; Vi.erase (Vi.begin ()+1);//deletes the element at the specified position        for(Iter=vi.begin (); Iter!=vi.end (); iter++) {cout<<*iter<<" "; } cout<<Endl; Vi.erase (Vi.begin ()+1, Vi.end ());//This is the deletion of the specified interval of the element, the interval is "I,j"        for(Iter=vi.begin (); Iter!=vi.end (); iter++) {cout<<*iter<<" "; } cout<<Endl; //Testing of vectorsVector <A>VEC; A;//initialize a struct elementA.id=1; A.length=2; A.width=3; Vec.push_back (a);//INSERT INTO VectorVector<a>:: iterator ite; Ite=Vec.begin (); cout<< (*ite) .id<<" "<< (*ite) .length<<" "<< (*ite) .width<<Endl;}

3. Here are some simple algorithms, there are other algorithms if necessary, please refer to the STL source analysis

(1) Flip the element with reverse: Requires a header file #include<algorithm>

Reverse (Vec.begin (), Vec.end ()); Flips the element (in a vector, if two iterators are required in a function,

Usually the latter is not included.)

(2) Sorting by using sort: Requires header file #include<algorithm>

Sort (Vec.begin (), Vec.end ());(by default in ascending order, that is, from small to large).

You can compare functions in descending order by overriding the sort comparison, as follows:

To define a sort comparison function:

BOOL Comp (const int &A,CONST int &b)
{
Return a>b;
}

Simple application of C++--vector

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.