forked from Bhupesh-V/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emplace.cpp
39 lines (30 loc) · 773 Bytes
/
emplace.cpp
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
/*
Author : Y. Sai Sriram
Date : 29/09/2019
Time : 10:53
Description : Places an element in the vector at the specified position
*/
#include <iostream>
#include <vector>
void print(const std::vector<int> &vec){
for(auto num : vec){
std::cout << num << " ";
}
}
int main(){
//creating a vector of 5 elements
std::vector<int> vector1{10, 20, 30, 40, 50};
std::cout << "Initial vector: ";
print(vector1);
std::cout << std::endl;
//add an element at the beginning
vector1.emplace(vector1.begin(), -10);
//add an element at the end
vector1.emplace(vector1.end(), 60);
//add an element after 2 elements
vector1.emplace(vector1.begin() + 2, 15);
std::cout << "Modified vector: ";
print(vector1);
std::cout << std::endl;
return 0;
}