English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Used to insert a new element at a specified position.
The syntax of vector (vector).insert() is:
insert(iterator,val); insert(iterator,n,val); insert(iterator,InputIterator first,InputIterator last);
iterator:The iterator defines the position where the new element will be inserted.
val:val is the value to be inserted.
n:The number of times this value appears.
(first,last):It defines the range of elements to be inserted.
It returns an iterator pointing to the newly inserted element.
The insert() function inserts a string into the vector.
#include<iostream> #include<vector> using namespace std; int main() { vector<string> v{"java"}; string str="programs"; v.insert(v.begin()+1,str); for(int i = 0; i < v.size(); i++) cout << v[i] << " \t "; return 0; }
Output:
java programs
In this example, the insert() function is used to insert the string "programs" into the vector (vector) "v".
Let's look at a simple example.
#include<iostream> #include<vector> using namespace std; int main() { vector<string> v{"C" , "Tutorials"}; v.insert(v.begin()+1,2,"C"); for(int i = 0; i < v.size(); i++) cout << v[i] << " \t "; return 0; }
Output:
C CC Tutorials
Let's look at a simple example.
#include<iostream> #include<vector> using namespace std; int main() { vector<int> v{1,2,3,4,5}; vector<int> v1{6,7,8,9,10}; v.insert(v.end(), v1.begin(), v1.begin()+5); for(int i = 0; i < v.size(); i++) cout << v[i] << " \t "; return 0; }
Output:
1 2 3 4 5 6 7 8 9 10