我有一些数据,每个数据项都有一个关联的键。 key 由两部分组成:我们称之为颜色和 ID。我想按颜色迭代容器以加快渲染速度,并且我还想仅通过 id 查找容器中的项目。
我尝试使用 std::map 来实现此目的
class MyKey {
public:
int color;
int id;
bool operator<(...)
bool operator==(...)
};
但我无法提供 < 运算符来保持数据按颜色排序,同时允许 map::find 单独处理 id(即没有有关颜色的信息)。
我希望插入和查找操作都很快(例如 O(log(n)))。
有什么想法可以使用什么样的容器来实现这个吗?
最佳答案
改编示例 here来自 Boost.Multi_index 基于以下修改:
typedef multi_index_container<
MyKey,
indexed_by<ordered_unique<identity<MyKey> >,
ordered_non_unique<member<MyKey,int,&MyKey::color> >
>
> DataSet;
关于c++ - 使用哪个排序的 STL 容器来通过特殊键进行快速插入和查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3734226/