-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.3 VectorInsert.cpp
More file actions
57 lines (44 loc) · 1.41 KB
/
17.3 VectorInsert.cpp
File metadata and controls
57 lines (44 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <vector>
#include <iostream>
using namespace std;
/*
// for older C++ compilers (non C++11 compliant)
void DisplayVector(const vector<int>& inVec)
{
for (vector<int>::const_iterator element = inVec.begin ();
element != inVec.end ();
++ element )
cout << *element << ' ';
cout << endl;
}
*/
void DisplayVector(const vector<int>& inVec)
{
for(auto element = inVec.cbegin(); // auto and cbegin() are C++11
element != inVec.cend (); // cend() is new in C++11
++ element )
cout << *element << ' ';
cout << endl;
}
int main ()
{
// Instantiate a vector with 4 elements, each initialized to 90
vector <int> integers (4, 90);
cout << "The initial contents of the vector: ";
DisplayVector(integers);
// Insert 25 at the beginning
integers.insert (integers.begin (), 25);
// Insert 2 numbers of value 45 at the end
integers.insert (integers.end (), 2, 45);
cout << "Vector after inserting elements at beginning and end: ";
DisplayVector(integers);
// Another vector containing 2 elements of value 30
vector <int> vecAnother (2, 30);
// Insert two elements from another container in position [1]
integers.insert (integers.begin () + 1,
vecAnother.begin (), vecAnother.end ());
cout << "Vector after inserting contents from another vector: ";
cout << "in the middle:" << endl;
DisplayVector(integers);
return 0;
}