c++ - 如何用适当的容器表示冲突?

标签 c++ containers

我有许多形状(类型为 Shape)。如果两个形状有重叠区域,它们就会相互冲突。我想用一个合适的容器来记录这些形状之间的所有冲突。我尝试的第一个是 std::set 一对,如下所示

std::set<std::pair<Shape*, Shape*>> conflicts;

// Add a conflict. Putting shape1 first means shape1 has priority over shape2.
conflicts.insert(make_pair(&shape1, &shape2));

// Add another where shapes is at the second.
conflicts.insert(make_pair(&shape3, &shape1));

但是,要获取与形状相关的所有冲突并不容易,例如 shape1,因为有时 shape1 会出现在冲突中的第二个。有什么好的方法吗?

最佳答案

基本上你有两个选择:

  1. 将两个形状 ab 之间的每个冲突插入到 std::multimap 中两次或 std::unordered_multimap所以 (a,b)(b,a)。 (set is dual to map)(你需要一个 multimap 这样你可以为每个形状存储多个冲突)

    在形状本身中维护列表/集合或使用从形状到列表/集合的映射在绿色中是一样的。

  2. 使用像 boost::multi_index_container 这样的东西.

    我希望这个解决方案实际上效率和舒适度较低。

关于c++ - 如何用适当的容器表示冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25096772/

相关文章:

Android 应用程序和开源库

c++ - : "warning: comparison between ' enum A<B >' and ' enum A<B >'"? 这是什么意思

image - 为什么 Podman 在从文件加载后尝试拉取已存在的镜像?

python - 如何从容器中删除相同的对象(但不一定是相同的对象)?

c++ - "CopyConstructible"对 C++ STL 容器元素的要求

C++继承模板类: hidden member variable

c++ - cout 覆盖了我的一些指针

c++ - 在 C++ 中返回 unique() 函数

vb.net - 如何在VB.NET中实现向导控件

templates - 使用 boost::graph 实现异构节点类型和边类型