C++ vector operation implementation

  • 2020-06-19 11:27:38
  • OfStack

In c++, vector is a 10-point useful container.

What it does: It can hold various types of objects like Container 1. Simply put, vector is a dynamic array that can hold any type of array and can add and compress data.

vector is part of the C++ Standard Template library, a multi-functional template class and function library that can manipulate a variety of data structures and algorithms.

Special attention:

The following points should be noted when using vector:

1, if you want to represent a long vector length (need to save a lot of internal vector), easy to cause memory leak, and low efficiency;

2. When Vector is used as the parameter or return value of the function, it should be noted that:

double Distance(vector < int > & a, vector < int > & b) where" & "Absolutely no less!!

1. vector Description:

vector is a vector type that can hold many types of data and is therefore referred to as a container Dynamic arrays are encapsulated classes.

vector header file -vector

vector initialization:

Methods 1.


vector<int>a(10);
// Define a 10 A vector of an integer element (the Angle bracket is the element type name, which can be any valid data type), does not have an initial value, whose value is uncertain 1

2.


vector<int>a(10,1);
// Define a 10 Is a vector of integer elements, and the initial value of each element given is 1

3.


vector<int>a(b);
// With the vector b To the vector a The assignment, a The value of theta is exactly the same thing as theta b The value of the 

4.


vector<int>a(b.begin(),b.begin+3);
// The vector b In the from 0-2 (total 3 Assign to elements of a . a The type of int type 

Method 5.


int b[7]={1,2,3,4,5,6,7};
vector<int> a(b,b+7 ) ;
 // Get the initial value from the array 

Several important operations on the vector object (for example)


#include<vector>
vector<int> a,b;
//b Is the vector, will b the 0-2 I'm going to assign each of these elements to the vector a
a.assing(b.begin(),b.begin()+3);
//a contains 4 A value of 2 The elements of the 
a.assing(4,2);
// return a At the end of the 1 An element 
a.back();
// return a The first 1 An element 
a.front();
// return a The first i The element , If and only if a There are 
a[i];
// empty a The elements in the 
a.clear();
// judge a Is null, null returns true , if not null false
a.empty();
// delete a The end of the vector 1 An element 
a.pop_back();
// delete a In the first 1 A (from the first 0 Count from) to no 2 Element, that is, delete the element from a.begin()+1 Count up (including it) 1 until a.begin()+3 (not including it) over 
a.erase(a.begin()+1,a.begin()+3);
// in a At the end of the 1 I'm going to insert the vectors 1 , whose value is 5
a.push_back(5);
// in a The first 1 Elements (from the first 0 Insert a value at the starting) position 5,
a.insert(a.begin()+1,5);
// in a The first 1 Elements (from the first 0 A calculated) position to insert 3 The value of each of them is zero 5
a.insert(a.begin()+1,3,5);
//b Is an array, in a The first 1 Elements (from the first 0 Insert the position of the element starting from) b The first 3 The number of elements to the number one 5 Elements (not included) b+6 ) 
a.insert(a.begin()+1,b+3,b+6);
// return a The number of elements in 
a.size();
// return a The total number of elements that can be held in memory 
a.capacity();
// will a Adjust the number of existing elements to 10 More is deleted, less is added, its value is random 
a.resize(10);
// will a Adjust the number of existing elements to 10 , more is deleted, less is added, its value is 2
a.resize(10,2);
// will a Is extended to 100 . 
a.reserve(100);
//b Is the vector, will a Sum of elements in b Element exchange as a whole 
a.swap(b);
//b Is vector, vector comparison operation and  != >= > <= <
a==b;

2. Several ways of sequential access to vector, with examples

** 2.1. Several ways to add elements to vector a **

1. Add elements to vector a


vector<int>a;
for(int i=0;i<10;++i){a.push_back(i);}

2. Select the element from the array to add to the vector


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

3. 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. Read elements from file to add to vector


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

5. Common error assignment methods


vector<int>a(10,1);
// Define a 10 Is a vector of integer elements, and the initial value of each element given is 1
0

2. Read elements from vectors

1. Obtained by subscript


vector<int>a(10,1);
// Define a 10 Is a vector of integer elements, and the initial value of each element given is 1
1

2. Read by iterator


vector<int>a(10,1);
// Define a 10 Is a vector of integer elements, and the initial value of each element given is 1
2

Several important algorithms


 #include<algorithm>
 // right a In the from a.begin() (including it) to a.end() The elements (excluding it) are arranged from small to large 
 sort(a.begin(),a.end());
 // right a In the from a.begin() (including it) to a.end() The elements of (excluding it) are inverted but not arranged, as in a Elements in the for 1,3,2,4, After inversion for 4,2,3,1
 reverse(a.begin(),a.end());
 // the a In the from a.begin() (including it) to a.end() The element (excluding it) is copied to b , from b.begin()+1 Starts copying, overwriting the original element 
 copy(a.begin(),a.end(),b.begin()+1);
 // in a In the from a.begin() (including it) to a.end() (does not include it) 10 , returns its position in the vector if it exists 
 find(a.begin(),a.end(),10);

Related articles: