C++ vector of vector using the method of of sequence access to the vector in a variety of ways)

  • 2020-04-02 01:57:52
  • OfStack

A vector is a vector type that can hold many types of data, such as several integers, so it is called a container. Vector is an important member of the C++ STL. To use it, you need to include header files:


#include<vector>;

One, the initialization of vector: there can be five ways, for example:

(1) the vector < int > A, (10). // defines a vector of 10 integer elements (the element type name in Angle brackets, which can be any valid data type), but does not give an initial value, whose value is uncertain.
(2) the vector < int > A (1, 1); // defines the vectors of 10 integer elements and gives each element an initial value of 1
(3) the vector < int > (b); // use the b vector to create the a vector
(4) the vector < int > A (b.b do v.begin (), b.b do v.begin + 3); // defines the value of a as the 0th through the second (three in all) element in b
(5) int b[7]={1,2,3,4,5,9,8}; The vector < int > A, b, b + 7); // gets the initial value from the array

2. Some important operations of vector object are illustrated as follows:

(1) a.assign(b.begin(), b.begin()+3); //b is a vector, assign a vector of 0 to 2 elements of b to a
(2) a.a ssign (4, 2); // is that a has only 4 elements, and each element is 2
(3) a. ack (); // returns the last element of a
(4) a.f ront (); // returns the first element of a
(5) a [I]; // returns the ith element of a if and only if a[I] exists 2013-12-07
(6) a.c Lear (); // empty the elements in a
(7) a.e mpty (); // returns true if a is null or false if it is not null
(8) Amy polumbo op_back (); // delete the last element of the a vector
(9) a.e rase (a. do v.begin () + 1, a. do v.begin () + 3); // delete the first element in a (from the 0th) to the second element, that is, the deleted element from a ()+1 (including it) to a (excluding it)+ 3 (excluding it)
Amy polumbo ush_back (10) (5); // insert an element after the last vector of a with a value of 5
(11) Anderson nsert (a. do v.begin () + 1, 5); // insert a value of 5 at the first element of a (from the 0th), such as 1,2,3,4, and 1,5,2,3,4
(12) Anderson nsert (a. do v.begin () + 1,3,5); // insert three Numbers at the position of the first element of a (from the 0th), each with a value of 5
(13) Anderson nsert (a. do v.begin () + 1, b + 3, + 6 b). //b is an array, which inserts the third element of b to the fifth element (excluding b+6) at the first element of a (counting from the 0th), such as b is 1,2,3,4,5,9,8, and then 1,4,5,9, 3,4,5,9,8
(14) a.s considering (); // returns the number of elements in a;
(15) a.c apacity (); // returns the total number of elements a can hold in memory
(16) a.r ezize (10); // adjust the number of existing elements of a to 10, delete more, complement less, its value is random
(17) a.r ezize (10, 2); // adjust the number of existing elements of a to 10, delete more, complement less, its value is 2
(18) a.r eserve (100); // expand a's capacity to 100, that is, now test a.acity (); This operation only makes sense if a large amount of data needs to be added to a, because it will avoid multiple memory expansion operations (when a's capacity is insufficient, the computer will automatically expand capacity, which of course inevitably reduces performance).
(19) a.s wap (b); //b is the vector that swaps the elements in a and b as a whole
(20) a = = b; //b is the vector, the comparison operation of the vector also! =, > =, < =, > . <

3. Several ways to access vector sequentially, as shown in the following examples:

1. Add elements to vector a


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


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 an element from an existing vector to add to the vector


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. You can also read elements from a file and add them to a vector


ifstream in("data.txt");
vector<int> a;
for(int i; in>>i)
a.push_back(i);

5. Myth


vector<int> a;
for(int i=0;i<10;i++)
a[i]=i;
//This approach and others like it are wrong. I made this mistake at first, but later found out that subscripts can only be used to get existing elements, and now a[I] is still an empty object

(2) read the elements from the vector
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 traverser


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<<" ";
 

Iv. Several important algorithms that need to include header files:


#include<algorithm>

(1) the sort (a. do v.begin (), a.e nd ()); // arrange the elements in a from a. boegin () (including it) to a. nd(not including it) from smallest to largest
(2) the reverse (a. do v.begin (), a.e nd ()); // invert, but do not arrange, elements in a from a. b1 () (including it) to a. d2 () (excluding it), such as 1,3,2,4 in a, 4,2,3,1
(3) copy (a. do v.begin (), a.e nd (), b.b do v.begin () + 1); // copy the elements in a from a. boegin () (including it) to a. nd() (excluding it) into b. copy from b. boegin ()+1 (including it), overwriting the original elements
(4) the find (a. do v.begin (), a.e nd (), 10); // find 10 in a from a () (including it) to a (not including it), and return its position in the vector if it exists


Related articles: