gpt4 book ai didi

c++ - 如何将 back_inserter 与转换结合起来,C++

转载 作者:可可西里 更新时间:2023-11-01 18:27:50 24 4
gpt4 key购买 nike

如何包装一个 OutputIterator,例如 back_inserter_iterator与转型?考虑

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)在返回 diff vector 之前:

具体来说,我如何存储 diff 元素(或迭代器)的地址而不是元素本身的地址。

std::vector<double&> diff;
auto baAdr = ??? std::back_inserter( ??? (diff));
std::set_difference(xx.begin(), xx.end(), yy.begin(), yy.end(), baAdr);

出于性能原因(实际数据很大)我不想构建临时 vector 和 std::transform从中。它也不适用于不可复制的可移动类型。

我可以使用 boost。

最佳答案

使用boost::function_output_iterator:

#include <vector>
#include <algorithm>
#include <boost/function_output_iterator.hpp>

int main()
{
std::vector<double> xx;
std::vector<double> yy;
std::vector<const double*> diff; // const pointers, or else you
// need a const_cast in lambda

std::set_difference(xx.begin(), xx.end(), yy.begin(), yy.end(),
boost::make_function_output_iterator(
[&diff](const double& d) { diff.push_back(&d); }
)
);
}

关于c++ - 如何将 back_inserter 与转换结合起来,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50090244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com