C++ C+中的类型转换迭代器+;

C++ C+中的类型转换迭代器+;,c++,boost,stl,iterator,C++,Boost,Stl,Iterator,我有类型SDK::TIPAddressDescription,我不控制它,我的THNNetIface也没有像从IDL规范生成的那样受到我足够的控制。此外,我还通过迭代器包装了类型,以使它们能够与STL一起工作 我想添加对现有代码的修改: // Update IP addresses of interfaces existing in DB vector<THNNetIface> modIfaces; set_intersection(ifaceFirstIter, ifaceLast

我有类型
SDK::TIPAddressDescription
,我不控制它,我的
THNNetIface
也没有像从
IDL
规范生成的那样受到我足够的控制。此外,我还通过迭代器包装了类型,以使它们能够与STL一起工作

我想添加对现有代码的修改:

// Update IP addresses of interfaces existing in DB
vector<THNNetIface> modIfaces;
set_intersection(ifaceFirstIter, ifaceLastIter, ipFirstIter, ipLastIter, 
    back_inserter(modIfaces), HNInfoComparator());
//更新数据库中现有接口的IP地址
矢量变换面;
集合交叉点(ifaceFirstIter、ifaceLastIter、ipFirstIter、ipLastIter、,
背面插入器(modIfaces),HNInfoComparator();
以下是:

// Note: delIfaces is not of type SDK::TIPAddressDescription as expected by STL;
vector<THNNetIface> delIfaces;
set_difference(ipFirstIter, ipLastIter, ifaceFirstIter, ifaceLastIter,
    mod_inserter(delIfaces), HNInfoComparator());
//注意:delIfaces不是STL预期的SDK::TIPAddressDescription类型;
向量面;
设置差异(ipFirstIter、ipLastIter、ifaceFirstIter、ifaceLastIter、,
mod_插入器(delIfaces),HNInfoComparator();
其中,
mod_迭代器
充当每个元素的
SDK::TIPAddressDescription
类型到
THNNetIface
的转换器(以满足STL要求)和
back\u插入器


如何在迭代器中进行这种类型转换?在类似的Boost库中是否有实现这一点的现有方法?

是的,Boost迭代器和Boost范围都有实现这一点的工具

  • 最通用的是
    boost::function\u output\u迭代器

    (c++03)


  • Boost Range
    transformed
    也许使用
    Boost::phoenix::construct
    会更优雅。遗憾的是,此选项不可用,因为
    set_difference
    需要一个输出迭代器。这是一个 思科
auto output = boost::make_function_output_iterator(
        phx::push_back(phx::ref(delIfaces), phx::construct<THNNetIface>(arg1)));

boost::set_difference(iface, ip, output, HNInfoComparator());
auto output = boost::make_function_output_iterator(
        [&](TIPAddressDescription const& ip) { delIfaces.emplace_back(ip); });

boost::set_difference(iface, ip, output, HNInfoComparator());