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());
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}
1#include <iostream>
2#include <vector>
3
4void print_vec(const std::vector<int>& vec)
5{
6 for (auto x: vec) {
7 std::cout << ' ' << x;
8 }
9 std::cout << '\n';
10}
11
12int main ()
13{
14 std::vector<int> vec(3,100);
15 print_vec(vec);
16
17 auto it = vec.begin();
18 it = vec.insert(it, 200);
19 print_vec(vec);
20
21 vec.insert(it,2,300);
22 print_vec(vec);
23
24 // "it" no longer valid, get a new one:
25 it = vec.begin();
26
27 std::vector<int> vec2(2,400);
28 vec.insert(it+2, vec2.begin(), vec2.end());
29 print_vec(vec);
30
31 int arr[] = { 501,502,503 };
32 vec.insert(vec.begin(), arr, arr+3);
33 print_vec(vec);
34}