Explanation of "C + +" vector usage

Source: Internet
Author: User

Transferred from: 56842637#commentbox

First, Introduction

The C + + vector class provides an alternative representation of the built-in array, which, like the string class, is part of the standard library and is used with a header file:

#include <vector>

Ii. Summary of the use of vectors:

1. Vector initialization: There can be five ways to illustrate the following:

(1) vector<int> a (10); A vector of 10 integer elements is defined (the element type name in angle brackets, which can be any valid data type), but no initial value is given, and its values are indeterminate.
(2) Vector<int>a (10,1); A vector of 10 integer elements is defined, and the initial value of each element is given as 1
(3) Vector<int>a (b); Use b vector to create a vector, the whole copy assignment value
(4) Vector<int>a (B.begin (), b.begin+3); Defines the No. 0 to 2nd (3) Elements of a value of B
(5) Intb[7]={1,2,3,4,5,9,8};vector<int> A (b,b+7); Get the initial value from the array

2. Several important operations of the vector object are illustrated below:

(1) a.assign (B.begin (), B.begin () +3),//b is a vector and assigns a vector of the 0~2 elements of B to a
(2) a.assign (4,2);//is a only 4 elements, and each element is 2
(3) a.back ();//Returns the last element of a
(4) A.front ();//returns the first element of a
(5) a[i];//Returns the first element of a, when and only if A[i] exists 2013-12-07
(6) a.clear ();//emptying the elements in a
(7) A.empty ();//Determine if A is empty, empty returns ture, and no null returns false
(8) A.pop_back ();//delete the last element of the A vector
(9)a.erase(A.begin () +1,a.begin () +3);//delete 1th in a (from No. 0) to 2nd element, i.e. delete element from A.begin () + 1 count (including IT) until A.begin () +3 (excluding it)
(Ten) A.push_back (5);//Insert an element after the last vector of a, with a value of 5
(one) A.insert (A.begin () +1,5);//The value 5 is inserted at the position of the 1th element of a (from the No. 0), such as A is 1,2,3,4, after the element is inserted 1,5,2,3,4
(a) A.insert (A.begin () +1,3,5);//Insert 3 numbers in the position of the 1th element of a (from No. 0), with a value of 5
(1) A.insert (A.begin () +1,b+3,b+6);//b An array that inserts the 3rd element of B into the 5th element (excluding b+6) in the position of the 1th element of a (from the No. 0), as in the case of the number B, in the number of elements (not including). 2,3,4,5,9,8, after inserting the element for 1,4,5,9,2,3,4,5,9,8
(+) a.size ();//Returns the number of elements in A;
(a.capacity);//Returns the total number of elements that a can hold in memory
(+) a.rezize (10);//The number of existing elements of A to 10, a lot of delete, less complement, its value is random. pay attention to the difference between resize and reserve!
(+) a.rezize (10,2);//Set the number of existing elements of a to 10, more delete, less complement, the value is 2
A.reserve (100);//Expand the capacity of a (capacity) to 100, that is, now Test a.capacity (); The return value is 100. This operation only makes sense if you need to add a lot of data to a. Because this avoids multiple capacity expansion operations in memory (the computer automatically expands when A's capacity is low, which certainly degrades performance)
(+) A.swap (b);//b is a vector that swaps the elements in a and the elements in B as a whole
(a==b),//b vector, vector comparison operation and!=,>=,<=,>,<

3. There are several ways to access vectors sequentially, as illustrated below:
(1) Adding elements to vector a

vector<int> A;  for (int i=0;i<; i++)    a.push_back (i);

(2) You can also select elements from the array to add to the vector  

int a[6]={1,2,3,4,5,6};vector <int>for (int i=1; i<=4; i++)    B.push_back (A[i]);

(3) You can also add an element to a vector from an existing vector.

int a[6]={1,2,3,4,5,6};vector <int> b;vector<int> C (a,a+4);
Vector<int> d (c); Use another vector c to initialize the current vector for (vector<int>::iterator it=c.begin (); It<c.end (); it++ ) b.push_back (*it);

(4) can also be read from the file element to the vector added

inch ("data.txt"); vector<int> A;  for (intin >>i)    a.push_back (i);

(5) two easy to make mistakes

vector<int>A; for(intI=0;i<Ten; i++) A[i]=i;//such practices and similar practices are all wrong. The subscript can only be used to get an existing element, and now the A[i] is an empty object//any insertion will increase the size of the vector, rather than overwriting an existing element, which may seem obvious, but the following errors are not uncommon among beginnersConst intSize =7; intia[size] = {0,1,1,2,3,5,8 }; Vector<int>Ivec (size); for(intIX =0; IX < size; ++ix) Ivec.push_back (ia[IX]); //at the end of the program, Ivec contains 14 elements, and the elements of IA are inserted from the eighth element .

4. Reading elements from vectors
(1) Read

by subscript

int a[6]={1,2,3,4,5,6};vector <int> B (a,a+4);  for (int i=0; i<=b.size ()-1; i++)    cout<<b[i]<<" ";

(2) read through the Walker mode

int a[6]={1,2,3,4,5,6};vector <int> B (a,a+4);  for (vector<int>::iterator it=b.begin (); It!=b.end (); it++)    cout<<*it<< " ";

three, several important algorithms, use need to include the header file:
#include <algorithm>
(1) sort (A.begin (), A.end ()); A small-to-large arrangement of elements in a from A.begin () (including IT) to A.end () (excluding it)
(2) reverse (A.begin (), A.end ()); The elements in a from A.begin () (including IT) to A.end () (excluding it) are inverted, but not arranged, such as the element in A is 1,3,2,4 and inverted to 4,2,3,1
(3) Copy (A.begin (), A.end (), B.begin () +1); Copy the element in a from A.begin () (including IT) to A.end () (excluding it) to B, starting from the position of B.begin () +1 (including IT), overwriting the original element
(4) Find (A.begin (), A.end (), 10); Find 10 in an element in a from A.begin () (including IT) to A.end () (excluding it) if there is a position in the vector that returns it

Iv. relevant examples

(1) Examples of classes and structures in which vectors are stored: Because vectors allow only one placeholder, the struct is plugged into the vector to compensate for the lack of vectors.

#include"stdafx.h"#include<vector>#include<string>using namespacestd;classaclass{ Public:    intnum; stringname;};structastruct {intnum; stringname;};voidteststruct () {//use of the classAClass Ac; Vector<AClass>VC; Ac.num=Ten; Ac.name="name";    Vc.push_back (AC);      AClass D;  for(Vector<aclass>::iterator it=vc.begin (); It<vc.end (); + +it) {D=*it; cout<<d.num<<Endl; }  //use of the structure bodyastruct as; Vector<AStruct>vs; As.num=Ten; As.name="name";    Vs.push_back (AS);      Astruct ds;  for(Vector<astruct>::iterator it=vs.begin (); It<vs.end (); + +it) {DS=*it; cout<<ds.num<<Endl; } }voidTestPoint () {//use of the classAClass *ac=NewAClass; Vector<aclass *>VC; Ac->num=Ten; Ac->name="name";    Vc.push_back (AC); AClass*D;  for(Vector<aclass*>::iterator it=vc.begin (); It<vc.end (); + +it) {D=*it; cout<<d->num<<Endl; } }int_tmain (intARGC, _tchar*argv[])    {teststruct ();    TestPoint (); intN; CIN>>N; return 0;}

(2) waiting to be replenished

Explanation of "C + +" 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.