Std::vector作为Std::set和Std::unordered_set中的键

std::vector as key in std::set and std::unordered_set

本文关键字:Std set vector 作为 unordered      更新时间:2023-10-16

我正在编写一个程序,计算棋盘游戏(即奥赛罗)中的大量位置。我的董事会代表是一个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呢?

c++中set和unordered_set的区别是什么?

基本上:除非你关心值的顺序,否则你可能应该使用unordered_set。

但对于所有其他性能问题:当有疑问时->开始测量。同时实现"less"answers"hash",然后用两个类尝试并检查哪个性能更好(注意:在这种情况下,你没有几乎相同的API。所以你不应该花很长时间来切换类进行测试)