1// where v is the vector to insert, i is
2// the position, and value is the value
3
4v.insert(v.begin() + i, v2[i])
1// CPP program to create an empty vector
2// and push values one by one.
3#include <vector>
4
5using namespace std;
6int main()
7{
8 // Create an empty vector
9 vector<int> vect;
10 //add/push an integer to the end of the vector
11 vect.push_back(10);
12 //to traverse and print the vector from start to finish
13 for (int x : vect)
14 cout << x << " ";
15
16 return 0;
17}
1// vector::push_back
2#include <iostream>
3#include <vector>
4
5int main ()
6{
7 std::vector<int> myvector;
8 int myint;
9
10 std::cout << "Please enter some integers (enter 0 to end):\n";
11
12 do {
13 std::cin >> myint;
14 myvector.push_back (myint);
15 } while (myint);
16
17 std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
18
19 return 0;
20}
1// inserting into a vector
2#include <iostream>
3#include <vector>
4
5int main ()
6{
7 std::vector<int> myvector (3,100);
8 std::vector<int>::iterator it;
9
10 it = myvector.begin();
11 it = myvector.insert ( it , 200 );
12
13 myvector.insert (it,2,300);
14
15 // "it" no longer valid, get a new one:
16 it = myvector.begin();
17
18 std::vector<int> anothervector (2,400);
19 myvector.insert (it+2,anothervector.begin(),anothervector.end());
20
21 int myarray [] = { 501,502,503 };
22 myvector.insert (myvector.begin(), myarray, myarray+3);
23
24 std::cout << "myvector contains:";
25 for (it=myvector.begin(); it<myvector.end(); it++)
26 std::cout << ' ' << *it;
27 std::cout << '\n';
28
29 return 0;
30}