Memo: Vector container in C + +

Source: Internet
Author: User

Use vector in C + +: container or called vector. It is convenient for data sets that are used to manage indefinite lengths or for frequently changing groups of data. It is easy to understand that he is a data group, a collection of data types.

A vector is a collection of objects of the same type . it is equivalent to a dynamic array, and when the programmer is unable to know the size of the array they need, it can be used to solve the problem to achieve maximum space savings.

Use vectors to first add a header file to the beginning of the program and use its name :

using std::vector;

the Declaration and initialization of vectors:

Vector < type > < name >

vector<int> intvector;

Vector<t> v1;

the vector holds an object of type T. The default constructor v1 is empty.

Vector<t> v2 (v1);

V2 is a copy of v1.

Vector<t> v3 (n, i);

the v3 contains n elements with a value of I .

Vector<t> v4 (n);

V4 contains n copies of elements that are initialized with values .

operation of the vector:

V.empty ()

returns True if V is null, otherwise false is returned.

V.size ()

Returns the number of elements in V.

V.push_back (t)

adds an element with a value of t at the end of V .

V[n]

Returns an element of position N in v .

V1 = V2

Replace the V1 element with a copy of the element in V2.

V1 = = V2

returns True if V1 is equal to V2.

! =, <, <=, >=

Keep these operators in their customary meaning.

Common functions:

1.Push_backAdd a data at the end of the array
2.Pop_backRemove the last data from the array
3.AtGet the data for the numbered position
4.BeginGet pointers to array headers
5.EndGet the last unit of the array +1The pointer
6. FrontGet a reference to an array header
7.backGet a reference to the last cell of the array
8.max_sizeGet vectorThe maximum can be how big
9.capacityCurrent vectorSize of allocation
10.SizeThe current size of the data used
11.ResizeChange the size of the current usage data, if it is larger than the current use, populate the default value
12.reserve the size of the allocated space
13. Erase 14. empty the current vector
15.rbegin Vector 16.rend Vector The end pointer of the inverted construct returns ( 17. Judge Vector is null
18.swap interchange data
19. insert insert a data

How vectors are used

1. Array idiom

vector can be used just like arrays

element can be accessed by ivec[index number]

intn[Ten]vector<int> Ivec (Ten);//array-like definition int ia[];vector<int> ivec2 (Ten,0);//set the initial value to 0ivec[0] = n[0];//Character Values for(intI=0; i<Ten; i++) cout<< Ivec[i] << Endl;

2.STL method of Use

An iterator iterator to access and manipulate elements. iterator iterator is a class in the standard library that has the function of pointers.

It is more convenient and more flexible to access the vector using an iterator.

Iterator declaration method:

vector < type >:: Iterator < name >

vector<int;:: iterator myiterator;

Cases:

vector<string> Svec (Ten,"NULL"); Vector<string>:: iterator n; n= Svec.begin ();//take the initial objectn = svec.begin () +2;//move to the specified object*n ="TEXT";//Character Values//The following statement redefined an iterator I and traversed the value of I.   for(vector<string>::iterator i = Svec.begin (); I! = Svec.end (); i++) {cout<< *i <<Endl; }

You can also define an empty vector with push_back, insert, resize, etc. to add or specify the size. Use him as you would with a dynamic array.

Use iterators as a way of using pointers:

vector<int*> Svec (Ten); Vector<int*>:: iterator N; intn[Ten] = {0,1,2,3,4,5,6,7,8,9}; int*p; intm =0;  for(vector<int*>::iterator i = Svec.begin (); I! = Svec.end (); i++) {p= &N[m];//give the pointer to P*i = p;//give the pointer to I or *I=&N[M]m++; cout<< *i <<'='<< *p <<'/'<< **i <<Endl; } N= Svec.begin () +2;//take a third iterator 0-1-2**n = -;//the value of the data character to which the iterator points.               equivalent to n[2]=100; //Note: *n is the saved address, and **n is the data value space that points to n[2]    for(M =0; M <Ten; m++) cout<< m <<'='<< N[m] << Endl;

Memo: Vector container in C + +

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.