c++ - how to use next_permutation -
i'm trying arrangement of tic tac toe boards. have following code:
// 5 turns x if x goes first std::string moves = "xxxxxoooo"; { std::cout << moves << std::endl; } while ( std::next_permutation(moves.begin(), moves.end()) ); but outputs original string once. i'm assuming each character has unique. what's way can this?
std::next_permutation returns next permutation in lexicographic order, , returns false if first permutation (in order) generated.
since string start ("xxxxxoooo") last permutation of string's characters in lexicographic order, loop stops immediately.
therefore, may try sorting moves before starting call next_permutation() in loop:
std::string moves = "xxxxxoooo"; sort(begin(moves), end(moves)); while (std::next_permutation(begin(moves), end(moves))) { std::cout << moves << std::endl; } here live example.
Comments
Post a Comment