C + + STL vector detailed

Source: Internet
Author: User

A. explanation:

Vector: is a sequential container, in fact the same as the array, but it is superior to arrays. In general, arrays cannot be expanded dynamically, so it is not a waste of memory when the program is running, which is causing cross-border. and Vector just makes up for this flaw, it is characterized by the equivalent of a scalable array, its random access fast, in the middle of insertions and deletions slow, but at the end of insertions and deletions Fast.

Two. usage:

1. Header Files

2. How to define

A) vector<int>v1; // vector elements are of type  int B) vector<string>v2; // vector element is of type  string C) vector<node>v3; // the queue element is a structural body, and the structure can be defined by itself

Vector<int>::iterator it; //define an iterator

3. Common operation

V1.push_back ()//add a data at the end of the arrayV1.pop_back ()//remove the last data from the array
V1.front () //return first element (top element of Stack)V1.begin ()//gets the pointer to the array header, which is accepted by the iteratorV1.end ()//get a pointer to the last cell +1 of the array, using an iterator to acceptV1.clear ()//Remove all data from the containerV1.empty ()//determine if the container is emptyV1.erase (POS)//Delete Data at the POS locationV1.erase (beg,end)//Delete data from the [beg,end] intervalV1.size ()//the number of actual data in the back container
V1.insert (pos,data)//insert data at POS

Three. Example

#include <iostream>#include<algorithm>#include<vector>using namespacestd;intmain () {vector<int> v;//Defining Vectorsvector<int>::iterator it;//define a vector iterator     for(inti =Ten; I >=1; I--)//Inserting DataV.push_back (i); cout<<"output:";  for(it=v.begin (); it!=v.end (); it++)//the value of the output iteratorcout<<*it<<" "; cout<<endl; It-=1; cout<<"the last value is:"<<*it<<"     "<<endl;                             V.erase (it); //Delete last elementcout <<"Number of Elements:"<<v.size () << endl;//number of output elementsSort (v.begin (), v.end ());//Vector Sortcout<<"after Sorting:";  for(it=v.begin (); it!=v.end (); it++)//Output vector elementscout << *it <<" "; cout<<endl; V.insert (v.begin (), -) ;//inserting a elem at the POS locationcout<<"the first element is:"<<v.front () <<endl;//outputs the first elementV.pop_back ();//remove the last elementcout <<"Number of Elements:"<<v.size () << endl;//number of output elementsV.clear ();//Vector Emptycout <<"number of elements after emptying:"<< v.size () << endl;//number of output elements    return 0;}

C + + STL vector detailed

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.