[英]std::vector as key in std::set and std::unordered_set
我正在编写一个评估棋盘游戏中大量姿势的程序(例如othello)。 我的董事会代表是std::vector
。 由于同一位置在处理中可能会出现更多次,因此我使用std::set<std::vector>
来存储检查的位置。
在性能方面,我不知道是否最好使用std::set
( 本机工作,因为std::vector
实现了所需的所有运算符)或std::unordered_set
具有良好的哈希函数(即boost::hash_range(...)
)。
另外,还有其他好的解决方案可以实现相同的目标吗? (也许是不同的电路板表示形式([可用时请确保位板]还是不同的数据结构?)
编辑******************************************编辑
这是获取板的伪代码:
enqueue initial board
while queue not empty:
dequeue a board
if board is already examined: //std::set search
continue
put board in examined boards
if board is solved:
print solution
else:
for each possible board that can arise out of this one:
add board to end of queue
看到
到底为什么有人会使用set而不是unordered_set?
基本上:除非您担心值的顺序,否则应该使用unordered_set。
但是对于所有其他性能问题:有疑问时->开始测量。 实现“ less”和“ hash”,然后在两个类中进行尝试,并检查性能是否更好(注意:在这种情况下,您几乎没有相同的API。因此,切换类不应该花很长时间进行测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.