【发布时间】:2018-10-09 22:37:32
【问题描述】:
如何使用转换包装像 back_inserter_iterator 这样的 OutputIterator?
考虑
std::vector<double> xx;
std::vector<double> yy;
std::vector<double> diff;
auto ba = std::back_inserter(diff);
std::set_difference(xx.begin(), xx.end(), yy.begin(), yy.end(), ba);
我想在推回差异向量之前应用一个免费函数f(double) 或g(std::vector<double>::iterator):
具体来说,我如何存储差异元素(或迭代器)的地址而不是元素本身。
std::vector<double&> diff;
auto baAdr = ??? std::back_inserter( ??? (diff));
std::set_difference(xx.begin(), xx.end(), yy.begin(), yy.end(), baAdr);
出于性能原因(实际数据很大),我不想从中构造一个临时向量和std::transform。它也不适用于不可复制的可移动类型。
我可以使用 boost。
【问题讨论】:
-
boost::function_output_itetator也许? -
@JohanLundberg 你看过example吗?就像,你真的想要
make_function_output_iterator([&](double d){ diff.push_back(f(d)); }) -
我认为 set_difference 不会将元素的地址提供给输出迭代器。也许如果您在
*iterator的operator=()中进行引用,但我不确定这是否得到保证,即您可能会获得临时地址。 -
@PaulR 我不认为复制实际值是允许的实现。而且,只能复制类型的子集。
-
我刚刚看了看,在 libstdc++ 7.2 中没有临时的。您是对的,额外的复制通常效率低下,因此不能在一般实现中使用。至于不可复制的类型,好吧,标准说:“复制元素......”,尽管使用正确的 operator= 你很高兴:-)