使用提升复制将结构的成员作为键映射到设置容器中
copy member of struct as key map into set container using boost copy
具有以下结构:
struct MixingParts{
int intPart;
double doublePart;
}
和 std::map 如下:
std::map<MixingParts, std::vector<int> > MixingMap;
我发现 boost::copy 非常有用,如果您请帮助我仅提取结构体的整数部分作为上述地图的键并将其插入回 std::set intSet,我将不胜感激;
boost::copy(MixingMap | boost::adoptors::map_keys(.....*Is it possible to use bind here?*...), std::inserter(intSet, intSet.begin())
我只能使用 C++98,并且赞赏任何其他不那么冗长和优化的解决方案。
boost::transform(mm, std::mem_fn(&MixingParts::intPart),
std::inserter(intSet, intSet.begin())));
或将copy
/copy_range
与transformed
适配器一起使用
下面是一个集成的实时示例:
住在科里鲁
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <iostream>
struct MixingParts{
int intPart;
double doublePart;
};
using boost::adaptors::transformed;
int main() {
std::vector<MixingParts> v { { 1,0.1 }, {2, 0.2}, {3,0.3} };
boost::copy(v | transformed(std::mem_fn(&MixingParts::intPart)),
std::ostream_iterator<int>(std::cout, " "));
}
指纹
1 2 3
boost::mem_fn
也可以使用。
for (MixingMap::const_iterator it = mm.begin(); it != mm.end(); ++it)
intSet.insert(it->first.intPart);
在 C++11 中它会不那么冗长,但按照 C++98 的标准,它几乎没有臃肿。 而且它简单且效率最佳(考虑到数据结构的选择)。
相关文章:
- 正在将无序映射设置为无序映射的值
- 从文件读取到映射中,该映射将键作为整数,并将相应的值设置为设置
- C++从 Java 角度设置和映射内存管理
- C++映射/设置迭代器不可递增错误
- 如何将字符设置为函数?我对语法感到困惑...设置<char>max_freq(映射<字符,整数>频率)
- C XTREE崩溃在出口上,映射/设置迭代器不可用
- 我如何将输入分配给在 for 循环中设置的映射功能
- 映射和设置总是一次分配一个项目吗
- 带有内存比较的 STL 映射.如何在运行时设置比较块大小
- c++映射/设置迭代器不可取消引用
- 映射/设置迭代器在 std::map 中不可递增
- 用于设置相关类属性的映射的合理键类型
- 映射/设置迭代器未解除可绑定运行时错误
- 调试断言失败映射/设置迭代器不可取消引用
- 使用提升复制将结构的成员作为键映射到设置容器中
- 映射/设置迭代器不可递增
- C++映射/设置迭代器在使用多映射时出现不可增加的错误
- 映射/设置迭代器不可递增映射/设置迭代器不可递增
- C++ 映射/设置迭代器不可取消引用 --- map<const char *,字符串>
- 映射/设置迭代器不兼容[c++]