Demonstrate insert_iterator in vector
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
vector<int> vectorObject;
vector<int>::iterator itr;
int i;
for(i = 0; i <5; i++)
vectorObject.push_back(i);
cout << "Original array: ";
itr = vectorObject.begin();
while(itr != vectorObject.end())
cout << *itr++ << " ";
cout << endl;
itr = vectorObject.begin();
itr += 2; // point to element 2
// create insert_iterator to element 2
insert_iterator<vector<int> > i_itr(vectorObject, itr);
*i_itr++ = 100; // insert rather than overwrite
*i_itr++ = 200;
cout << "Array after insertion: ";
itr = vectorObject.begin();
while(itr != vectorObject.end())
cout << *itr++ << " ";
return 0;
}
Related examples in the same category