Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

vector insert() function in C++ STL

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

int main() {
   vector<int> v = { 50,60,70,80,90},v1;        //declaring v(with values), v1 as vector.
   vector<int>::iterator iter;                  //declaring an iterator
   iter = v.insert(v.begin(), 40);              //inserting a value in v vector before the beginning.
   iter = v.insert(v.begin(), 1, 30);           //inserting a value with its size in v vector before the beginning.
   cout << "The vector1 elements are: \n";
   for (iter = v.begin(); iter != v.end(); ++iter)
      cout << *iter << " "<<endl;             // printing the values of v vector
   v1.insert(v1.begin(), v.begin(), v.end()); //inserting all values of v in v1 vector.
   cout << "The vector2 elements are: \n";
   for (iter = v1.begin(); iter != v1.end(); ++iter)
      cout << *iter << " "<<endl;            // printing the values of v1 vector
   return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.