C++ 为什么std::next_置换会跳过一些置换?

C++ 为什么std::next_置换会跳过一些置换?,c++,permutation,C++,Permutation,“opt”(或这3个字母的任意组合)的预期输出应为“opt”、“top”和“pot”(按任意顺序),但此程序仅查找并匹配“top”和“pot”。下一个排列似乎永远不会超过“opt”,我也不确定问题出在哪里。当试图找到“apt”的所有排列时,同样的问题也会出现 #包括 #包括 #包括 #包括 #包括 使用名称空间std; //int loadDictionary(istream和dictfile、vector和dict); int排列(字符串、向量和dict、向量和结果){ 排序(word.beg

“opt”(或这3个字母的任意组合)的预期输出应为“opt”、“top”和“pot”(按任意顺序),但此程序仅查找并匹配“top”和“pot”。下一个排列似乎永远不会超过“opt”,我也不确定问题出在哪里。当试图找到“apt”的所有排列时,同样的问题也会出现

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//int loadDictionary(istream和dictfile、vector和dict);
int排列(字符串、向量和dict、向量和结果){
排序(word.begin(),word.end());
while(下一个排列(word.begin(),word.end()){

cout
std::next_permutation
进行排列,因此常规用法是使用
do while

int permute(std::string word, std::vector<std::string>& dict, std::vector<std::string>& results) {
    std::sort(word.begin(), word.end());
    do {
        std::cout << "Permutation: " << word << std::endl;
        if (std::binary_search(dict.begin(), dict.end(), word)) {
            std::cout << "Match found: " << std::word << std::endl;
            results.push_back(word);
    } while (next_permutation(word.begin(), word.end()));
    int matches = results.size();
    return matches;
}
int排列(std::字符串字、std::向量和dict、std::向量和结果){
排序(word.begin(),word.end());
做{

std::cout
while(下一个排列(word.begin(),word.end())
第12行将在循环第一次进入之前排列单词,这会导致第一次排列丢失。解决此问题的一种方法是将while循环更改为do-while循环,这将通过在实际排列之前处理初始单词来解决问题。请参阅@Telescope谢谢,将其更改为do while循环修复了它。从未想过使用它,因为我认为while循环通常会起作用,这是多余的。我知道这超出了问题的范围,但你知道为什么二进制搜索与某些单词不匹配吗?它可以与线性搜索配合使用,但显然效率要低得多。二进制搜索需要容器正在搜索以进行排序。我猜不是。@JohnFilleau文本文件包含25000个按字母顺序排序的单词,所以我认为不是这样。我使用cout进行了双重检查。
int permute(std::string word, std::vector<std::string>& dict, std::vector<std::string>& results) {
    std::sort(word.begin(), word.end());
    do {
        std::cout << "Permutation: " << word << std::endl;
        if (std::binary_search(dict.begin(), dict.end(), word)) {
            std::cout << "Match found: " << std::word << std::endl;
            results.push_back(word);
    } while (next_permutation(word.begin(), word.end()));
    int matches = results.size();
    return matches;
}
std::map<std::string, std::vector<string>> make_anagram_dict(const std::vector<string>& words)
{
    std::map<std::string, std::vector<string>> res;

    for (const auto& word : words) {
        auto anagram = word;
        std::sort(anagram.begin(), anagram.end());
        res[anagram].push_back(word);
    }
    return res;
}
int main() {
    std::vector<string> words;
    ifstream fileName("words.txt");
    loadDictionary(fileName, words);
    auto dict = make_anagram_dict(words);
    do {
       std::cout << "\nEnter a string for a anagram: ";
       std::string anagram;
       std::cin >> anagram;
       std::sort(anagram.begin(), anagram.end());
       auto it = dict.find(anagram);
       if (it != dict.end()) {
           for (const auto& word : it->second) {
               std::cout << "Match found: " << word << std::endl;
           }
       }
    } while (std::cin); // or other conditions to break the loop
    std::cout << "\nMain complete\n";
}