简体   繁体   中英

using insert iterators without std::copy

I have an insert iterator and an iterator to the element I want to insert but I am not allowed to use std::copy .

This is what I found on the c++ reference page:

std::copy (bar.begin(),bar.end(),insert_it);

This is also what I want to do but I can't use std::copy . Is there another way?

auto first = bar.begin();
auto last = bar.end();
while (first!=last)
{
  *insert_it = *first;
  ++insert_it;
  ++first;
}

The code you provides moves the content from bar.begin() to bar.end() into the insert iterator. I'm confused by the term you used insert and std::copy will not insert anything , just copy content.

To actually insert elements in the container you can use this:

your_container.insert(insert_it, bar.begin(),bar.end());

You can find an example here for std::vector http://en.cppreference.com/w/cpp/container/vector/insert/insert/

Hope this helps, Raxvan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM