c++ - 允许指定单独的比较器和排序仿函数的关联容器

标签 c++ boost dictionary stl set

我正在寻找一个允许我为排序和等价指定单独函数的容器。 std::set 允许指定 1 个比较器函数。 理想情况下,我想根据一个条件进行排序,但通过搜索集合,我想使用另一个条件进行匹配。我通过使用存储插入集合的迭代器的映射来破解这个解决方案。但我想知道是否有更好的方法。

最佳答案

试试 boost::multi_index_container:http://www.boost.org/doc/libs/1_53_0/libs/multi_index/doc/index.html

The Boost Multi-index Containers Library provides a class template named multi_index_container which enables the construction of containers maintaining one or more indices with different sorting and access semantics. Indices provide interfaces similar to those of STL containers, making using them familiar. The concept of multi-indexing over the same collection of elements is borrowed from relational database terminology and allows for the specification of complex data structures in the spirit of multiply indexed relational tables where simple sets and maps are not enough. A wide selection of indices is provided, modeled after analogous STL containers like std::set, std::list and hashed sets.

Boost.MultiIndex features additional functionalities, like subobject searching, range querying and in-place updating of elements, which make it a convenient replacement for std::set and set::multiset even when no multi-indexing capabilities are needed.

它完全支持您正在寻找的东西。请参阅有关 multiple sorting 的部分.

关于c++ - 允许指定单独的比较器和排序仿函数的关联容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16282572/

相关文章:

c++ - 初始化列表之前的对象初始化

c++ - 在 C++ 中使用 boost::split 的段错误(核心转储)

带有 Boost 库的 C++。读书专栏。 Excel/CSV文件

python - 相应的字典键和值输出

python - 将 python 字典表示为矩阵或 csv

C++ 代码输出负值

c++ - 检查字符串是否只包含字母

c++ - srand(1) 和 srand(0) 有什么区别

c++ - Boost shared_ptr 不会立即销毁对象

python - 在Python中,为什么这一行从文件中运行(无异常),但在shell中运行时抛出异常?