我有 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());
具有以下内容:
// 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());
其中 mod_iterator
充当从 SDK::TIPAddressDescription
类型到每个元素的 THNNetIface
的转换器(以满足 STL 要求)和 back_inserter
同时(或与它们兼容)。
如何做这个类型转换迭代器?在类似 Boost 的库中是否存在这样做的现有方法?
最佳答案
是的,Boost Iterator 和 Boost Range 具有执行此操作的功能。
最通用的是
boost::function_output_iterator
Live On Coliru (c++03)
auto output = boost::make_function_output_iterator( phx::push_back(phx::ref(delIfaces), phx::construct<THNNetIface>(arg1))); boost::set_difference(iface, ip, output, HNInfoComparator());
或
Live On Coliru (c++11)
auto output = boost::make_function_output_iterator( [&](TIPAddressDescription const& ip) { delIfaces.emplace_back(ip); }); boost::set_difference(iface, ip, output, HNInfoComparator());
boost 范围遗憾的是这个选项不可用,因为transformed
也许与boost::phoenix::construct<>
可能更优雅set_difference
需要一个输出迭代器。这是一个 想
关于c++ - C++ 中的类型转换迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28114181/