简体   繁体   中英

std::set with unconsistent operator<

I'm writing a C++ algorithm to solve a board game. The solution is based on the following:

enqueue initial board
while queue not empty:
    dequeue a board
    if board is solved:
        print solution
    else:
        for each possible board that can arise out of this one:
            add board to end of queue

Since I don't want to examine the same board more than one time I use std::set<Board> to keep track of examined boards.

In Board class bool operator<(const Board& rhs) const is defined to make std::set work properly.

So what happens in my std::set if my comparison function doesn't ensure an order in board instances?

As example:

a = Board()
b = Board()
c = Board()

a > b returns true
b > c returns true
a > c returns false

Is it possible that std::set , since it's based on Red-Black Tree , inserts the same Board more than once?

If the comparator doesn't work properly, the structure won't work properly. It might report that items are missing. It might fail to insert something. It could crash quickly, or it could appear to work for all your test cases and then crash on the customer's machine.

All bets are off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM