Check out example codes for "Insert into vector C++". It will help you in understanding the concepts better.

Code Example 1

// Program below illustrates the 
// vector::insert() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialising the vector 
    vector<int> vec = { 10, 20, 30, 40 }; 
  
    // inserts 3 at front 
    auto it = vec.insert(vec.begin(), 3); 
    // inserts 2 at front 
    vec.insert(it, 2); 
  
    int i = 2; 
    // inserts 7 at i-th index 
    it = vec.insert(vec.begin() + i, 7); 
  
    cout << "The vector elements are: "; 
    for (auto it = vec.begin(); it != vec.end(); ++it) 
        cout << *it << " "; 
  
    return 0; 
}

Code Example 2

vector_name.push_back(element_to_be_added);

Code Example 3

#include<bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> vec = {20, 30, 50, 60};
    vec.insert(vec.begin()+2, 40);
    for(auto x:vec)
    {
        cout << x << " ";
    }

    return 0;
}

Code Example 4

// CPP program to create an empty vector 
// and push values one by one. 
#include <vector>

using namespace std;
int main() 
{ 
    // Create an empty vector 
    vector<int> vect;  
    //add/push an integer to the end of the vector
    vect.push_back(10); 
	//to traverse and print the vector from start to finish
    for (int x : vect) 
        cout << x << " ";

    return 0; 
}

Code Example 5

std::vector<int> v;
v.push_back(10);

Code Example 6

// inserting into a vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (3,100);
  std::vector<int>::iterator it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 );

  myvector.insert (it,2,300);

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  std::vector<int> anothervector (2,400);
  myvector.insert (it+2,anothervector.begin(),anothervector.end());

  int myarray [] = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray, myarray+3);

  std::cout << "myvector contains:";
  for (it=myvector.begin(); it<myvector.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Learn ReactJs, React Native from akashmittal.com