-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path88insert_in_vector.cpp
More file actions
38 lines (33 loc) · 1.32 KB
/
88insert_in_vector.cpp
File metadata and controls
38 lines (33 loc) · 1.32 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
#include<iostream>
#include<vector>
using namespace std;
void display(vector<int> &v){ // vector<int> &v : syntax for vector as parameter
for(int i=0; i<v.size(); i++){
// cout<<v[i]<<" ";
cout<<v.at(i)<<" "; // v.at(i) is same as v[i]
}
cout<<endl;
}
int main(){
vector<int> vec1; // syntax for vector, notice how we don't have to mention a size
int size, element;
cout<<"Enter the size of your vector: ";
cin>>size;
for(int i=0; i<size; i++){
cout<<"Enter an element to add to the vector: ";
cin>>element;
vec1.push_back(element); // to add element at the end of the vector
}
vector<int> :: iterator i = vec1.begin(); // defining an iterator i
// in the scope of the vector class
// i = vec1.begin() : i points to first position of the vector vec1
display(vec1);
vec1.insert(i, 566); // insert 566 at position i of the vector vec1
display(vec1);
vec1.insert(i, 5, 88); // insert 88 five times from position i in the vector vec1
display(vec1);
// // NO IDEA WHAT THE PROBLEM IS WITH THIS LINE OF CODE
// vec1.insert(i+4, 66); // insert 66 at 4th position from i in the vector vec1
// display(vec1);
return 0;
}