c++ vector common function example parsing

  • 2020-10-23 21:09:06
  • OfStack

c++ vector commonly used functions

[

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

]

vector is also 1 array but its footprint is dynamic. When the vector footprint is full, the memory is reallocated and assigned to all the original elements, in order to avoid frequent reallocation, the data is migrated. vector actually allocates more memory than you need. For example, if you have 10 int data in vector, the actual memory occupied by vector is 20 int memory. When your data consumption exceeds the proportion of actual memory consumption, vector will automatically reallocate the memory and transfer the data. The actual memory occupied by vector can be viewed by USING capacity()


#include<iostream>
#include<vector>
using namespace std;
int main(){
  vector<int> ans;
  for(int i=0; i<10; i++) ans.push_back(i);
  ans.erase(ans.begin()+2);
  cout<<" Erase the first 3 A number: ";
  for(int j=0; j<ans.size(); j++) cout<<ans[j]<<" ";
  ans.erase(ans.begin(), ans.begin()+2);
  cout<<endl<<" Erase the former 2 A number: ";
  for(int k=0; k<ans.size(); k++) cout<<ans[k]<<" ";
  // Try not to use this function too often. It can cause a lot of data movement and reduce program efficiency 
  ans.insert(ans.begin()+1, 100);
  cout<<endl<<" In the first 1 Bit after insertion 100 : ";
  for(int m=0; m<ans.size(); m++) cout<<ans[m]<<" ";
  //vector At declaration time, the size and default values can be declared 
  vector<int> temp(5, -1);
  cout<<endl<<"temp The size of 5 , the default value is -1 : ";
  for(int l=0; l<temp.size(); l++) cout<<temp[l]<<" ";
  //resize(int n) change vector The number of data actually stored,   if n More than the actual number of bits added 0 Otherwise intercept the excess data 
  temp.resize(8);
  cout<<endl<<" the temp Change the size of a bit 8 : ";
  for(int h=0; h<temp.size(); h++) cout<<temp[h]<<" ";
  // In the change vector The size can also specify the value of the extra memory; This only works if you have more space allocated than you originally had 
  temp.resize(10, 1111);
  cout<<endl<<"temp Change the size of 10 , and specifies the value bit of the extra space 11111 : ";
  for(int g=0; g<temp.size(); g++)cout<<temp[g]<<" ";
  cout<<endl<<" To obtain temp The first 1 An element: "<<temp.front()<<endl<<" To obtain temp At the end of the 1 An element: "<<temp.back();
  // The commonly used empty() and size Function to determine vector Is null, when vector When it's empty,  empty() return true .  size() The value of 0
return 0;}

In addition, #include can be used < algorithm > The unique function in the library removes duplicate elements in vector


vector<int> ans;
ans.erase(unique(ans.begin(), ans.end()), ans.end());

Related articles: