devxlogo

Using Insert Iterators

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 vecInt;

For example, the following statement will fail because vecInt is not allocated space and the unique_copy() algorithm uses this assignment operator:

#include 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 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().

See also  5 Tips for Choosing the Right Medical Software for Your Practice
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist