Source: http://www.jb51.net/article/44231.htm
Font: [Increase decrease] Type: Reprint time: 2013-12-08 I want to comment
Vectors are vector types that can hold many types of data, such as several integers, so call it a container, this article describes how to use
Vectors are vector types that can hold many types of data, such as several integers, so they are called containers. Vector is an important member of C + + STL, which needs to include a header file:
Copy CodeThe code is as follows:
#include <vector>;
First, vector initialization: There can be five ways, for example, 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) int b[7]={1,2,3,4,5,9,8};vector<int> A (b,b+7); Get the initial value from the array
Second, the vector object of several important operations, examples are as follows:
(1) a.assign (B.begin (), B.begin () +3); b is a vector that assigns a vector of 0~2 elements of B to a
(2) A.assign (4,2); Is a contains 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 (); Empty the elements in a
(7) A.empty (); Determines whether a is empty, NULL 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 the 1th (from the No. 0) to the 2nd element in a, which means that the deleted element is counted from A.begin () +1 (including IT) until A.begin () + 3 (excluding it)
(Ten) A.push_back (5); Inserts an element after the last vector of a, with a value of 5
(one) A.insert (A.begin () +1,5); Inserts the value 5 in 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.insert) (A.begin () +1,3,5); Inserts 3 numbers in the position of the 1th element of a (from the No. 0), with a value of 5
(A.insert) (A.begin () +1,b+3,b+6); b is 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 B is 1,2,3,4,5,9,8, after the element is inserted 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 a can hold in memory
(+) A.rezize (10); The number of existing elements of A is adjusted to 10, many are deleted, less complement, its value is random
(+) a.rezize (10,2); The number of existing elements of A is adjusted to 10, many are deleted, less is complementary, the value is 2
(A.reserve) (100); Expand the capacity of a (capacity) to 100, which means that the a.capacity () is now tested and 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, and the elements in a and the elements in B are exchanged in their entirety
(A==B); b is vector, vector comparison operation and!=,>=,<=,>,<
Three, sequential access to the vector of several ways, for example, the following:
1. Adding elements to vector a
Copy CodeThe code is as follows:
Vector<int> A;
for (int i=0;i<10;i++)
A.push_back (i);
2, you can also select elements from the array to add to the vector
Copy CodeThe code is as follows:
int a[6]={1,2,3,4,5,6};
Vector<int> b;
for (int i=1;i<=4;i++)
B.push_back (A[i]);
3. You can also add an element from an existing vector to the vector.
Copy CodeThe code is as follows:
int a[6]={1,2,3,4,5,6};
Vector<int> b;
Vector<int> c (a,a+4);
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
Copy CodeThe code is as follows:
Ifstream in ("Data.txt");
Vector<int> A;
for (int i; in>>i)
A.push_back (i);
5, "misunderstanding"
Copy CodeThe code is as follows:
Vector<int> A;
for (int i=0;i<10;i++)
A[i]=i;
Such practices and similar practices are all wrong. I made this mistake at first, and later found out that the subscript can only be used to get an existing element, and now the A[i] is an empty object.
(2) reading elements from vectors
1. Read by subscript method
Copy CodeThe code is as follows:
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 by the way of the ergodic device
Copy CodeThe code is as follows:
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<< "";
Four, several important algorithms, the use of the need to include a header file:
Copy CodeThe code is as follows:
#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
C + + vector (vector) method of use (multiple ways to access vectors sequentially)