C + + vector (vector) use of methods to explain (sequential access vector in a variety of ways) _c language

Source: Internet
Author: User

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 the C + + STL, and you need to include the header file when you use it:

Copy Code code as follows:

#include <vector>;

Vector initialization: There are five ways to do this, examples are as follows:

(1) vector<int> a (10); A vector that defines 10 integral elements (the element type name in angle brackets, which can be any legitimate data type), but does not give an initial value and its values are indeterminate.
(2) vector<int> A (10,1); A vector that defines 10 integer elements, and gives an initial value of 1 for each element.
(3) vector<int> A (b); b vector to create a vector, the whole copy of the assignment
(4) Vector<int> A (B.begin (), b.begin+3); The No. 0 to 2nd (total 3) elements of a value B are defined
(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, several important operations of vector objects, examples are as follows:

(1) a.assign (B.begin (), B.begin () +3); b is a vector that assigns a vector of B's 0~2 elements 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 (); To determine whether a is empty, empty to return ture, or null to return False
(8) A.pop_back (); Delete the last element of a vector
(9) A.erase (A.begin () +1,a.begin () +3); Delete the 1th in a (from the No. 0) to the 2nd element, which means that the deleted element is counted from A.begin () +1 (including IT) until A.begin () + 3 (excluding it)
(a) A.push_back (5); Inserts an element after the last vector of a, with a value of 5
(a) A.insert (A.begin () +1,5); Inserts a value of 5 in the position of the 1th element of a (starting from No. 0), as a is 1,2,3,4, and the element is inserted after the 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 (starting from No. 0), with a value of 5
(A.insert) (A.begin () +1,b+3,b+6); b in an array, insert the 3rd element of B into the 5th element (excluding b+6) in the position of the 1th element of a (starting from No. 0), as B is 1,2,3,4,5,9,8, and after inserting the element as 1,4,5,9,2,3,4,5,9,8
(a) 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, many are deleted, less complement, its value is random
(a) a.rezize (10,2); The number of the existing elements of a to 10, many are deleted, a little complement, the value of 2
(a) A.reserve (100); Expand the capacity of a (capacity) to 100, which means that you are now testing a.capacity () and the return value is 100. This operation only makes sense if you need to add a lot of data to a. Because this will avoid a multiple capacity expansion operation of memory (when A's capacity is low, the computer will automatically expand, of course, this must degrade performance)
(a) A.swap (b); b is a vector, the elements in a and the elements in B are exchanged as a whole
(a) a==b; b is vector, comparison of vectors, and!=,>=,<=,>,<.

Three, the sequential access vector of several ways, examples are as follows:

1. Add elements to vector a

Copy Code code 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 Code code 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 select elements from the existing vector to add to the vector

Copy Code code 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 to add elements to the vector

Copy Code code as follows:

Ifstream in ("Data.txt");
Vector<int> A;
for (int i; in>>i)
A.push_back (i);

5, "misunderstanding"

Copy Code code as follows:

Vector<int> A;
for (int i=0;i<10;i++)
A[i]=i;
This and similar practices are wrong. I have made this mistake at first, and later found that the subscript can only be used to get an existing element, and now the a[i is still an empty object

(2) reading elements from vectors
1, through the subscript way to read

Copy Code code 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, through the traversal of the way to read

Copy Code code 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 Code code as follows:

#include <algorithm>

(1) Sort (A.begin (), A.end ()); The elements from A.begin () (including IT) to A.end () (excluding it) are arranged from small to large in a
(2) Reverse (A.begin (), A.end ()); Invert the elements in a from A.begin () (including IT) to A.end () (excluding it), but do not arrange, as if the element in a is 1,3,2,4, and the inversion is 4,2,3,1
(3) Copy (A.begin (), A.end (), B.begin () +1); Copy the elements from A.begin () (including IT) to A.end () (excluding it) into B, starting with the position of B.begin () +1 (including IT), overwriting the original element
(4) Find (A.begin (), A.end (), 10); In a, look for 10 in an element from A.begin () (including IT) to A.end () (excluding it), if there is a return to its position in the vector

Related Article

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.