emplace(): It is a build-in function in STL. It inserts an object in a vector before a specified position. It returns
an iterator that points to the inserted data.
Parameters:
position: It is the constant iterator to the vector.
value: It is the value to be inserted.
Syntax:
vecName.empace(position, value)
Note: This kind of operation are expensive for a vector.
For Example:
vec= {1,2,4,5}
vec.emplace(vec.begin()+2,3) = > vector becomes {1,2,3,4,5}
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){vector<int> vec = {1, 2, 4, 5};auto it = vec.begin();it = it + 2;vec.emplace(it, 3);for (int i = 0; i < vec.size(); i++)cout << vec[i] << " ";return 0;}
No comments:
Post a Comment