how to use next_permutation

前端 未结 1 2021
时光说笑
时光说笑 2020-12-20 14:09

I\'m trying to get an arrangement of tic tac toe boards. So I have the following code:

// 5 turns for x if x goes first
std::string moves = \"xxxxxoooo\";

d         


        
相关标签:
1条回答
  • 2020-12-20 14:48

    std::next_permutation returns the next permutation in lexicographic order, and returns false if the first permutation (in that order) is generated.

    Since the string you start with ("xxxxxoooo") is actually the last permutation of that string's characters in lexicographic order, your loop stops immediately.

    Therefore, you may try sorting moves before starting to call next_permutation() in a loop:

    std::string moves = "xxxxxoooo";
    sort(begin(moves), end(moves));
    
    while (std::next_permutation(begin(moves), end(moves)))
    {
        std::cout << moves << std::endl;
    }
    

    Here is a live example.

    0 讨论(0)
提交回复
热议问题