我有许多形状(类型为 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 会出现在冲突中的第二个。有什么好的方法吗?
最佳答案
基本上你有两个选择:
将两个形状
a
和b
之间的每个冲突插入到std::multimap
中两次或std::unordered_multimap
所以(a,b)
和(b,a)
。 (set is dual to map)(你需要一个 multimap 这样你可以为每个形状存储多个冲突)在形状本身中维护列表/集合或使用从形状到列表/集合的映射在绿色中是一样的。
使用像 boost::multi_index_container 这样的东西.
我希望这个解决方案实际上效率和舒适度较低。
关于c++ - 如何用适当的容器表示冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25096772/