Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Dec 7, 2004



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using Insert Iterators

Insert iterators come in very handy when you need to populate a container using an STL algorithm which uses an assignment operator to fill in the data:

int iArr [] = {2, 2, 3, 4, 5, 6, 7};

#include <vector>
vector<int> vecInt;
For example, the following statement will fail because vecInt is not allocated space and the unique_copy() algorithm uses this assignment operator:

#include <algorithm>
unique_copy (iArr, iArr+7, vecInt.begin());
Conversely, using back_inserter() as the third argument causes the container to invoke push_back() instead of the assignment operator.

#include <iterator>
unique_copy (iArr, iArr+7, back_inserter(vecInt));
The other kind of insert iterator is front_inserter(). Be sure before using these that the underlying container supports the functions they invoke in place of the assignment operator. For instance, push_front() in place of the assignment operator, and vector does not support push_front().
Saurabh Ramya
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date