简体   繁体   English

使用不带std :: copy的插入迭代器

[英]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 . 我有一个插入迭代器和一个要插入的元素的迭代器,但不允许使用std::copy

This is what I found on the c++ reference page: 这是我在c ++参考页上找到的:

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

This is also what I want to do but I can't use std::copy . 这也是我想要做的,但是我不能使用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. 您提供的代码将内容从bar.begin()移到bar.end()到插入迭代器中。 I'm confused by the term you used insert and std::copy will not insert anything , just copy content. 我对您使用insertstd::copy这个术语感到困惑,它不会插入任何内容,而只是复制内容。

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/ 您可以在此处找到std::vector的示例http://en.cppreference.com/w/cpp/container/vector/insert/insert/

Hope this helps, Raxvan. 希望这会有所帮助,Raxvan。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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