1vector<int> a;
2vector<int> b;
3// Appending the integers of b to the end of a
4a.insert(a.end(), b.begin(), b.end());
1Input:
2 vector<int> v1{ 10, 20, 30, 40, 50 };
3 vector<int> v2{ 100, 200, 300, 400 };
4
5 //appending elements of vector v2 to vector v1
6 v1.insert(v1.end(), v2.begin(), v2.end());
7
8 Output:
9 v1: 10 20 30 40 50 100 200 300 400
10 v2: 100 200 300 400
1//vector.push_back is the function. For example, if we want to add
2//3 to a vector, it is just vector.push_back(3)
3vector <int> vi;
4vi.push_back(1); //[1]
5vi.push_back(2); //[1,2]
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}