变位解算器在c++

Anagram solver in C++

本文关键字:c++      更新时间:2023-10-16

我正在为学校做一个项目,我被卡住了,我认为只是很小的一部分,但我想不出来。

到目前为止我写的是:

#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
    set<string> setwords;
    ifstream infile;
    infile.open("words.txt"); //reads file "words.txt"
    string word = argv[1]; // input from command line
    transform(word.begin(), word.end(), word.begin(), tolower); // transforms word to lower case.
    sort(word.begin(), word.end()); // sorts the word
    vector<string> str; // vector to hold all variations of the word
    do {
        str.push_back(word);
    }
    while (next_permutation(word.begin(), word.end())); // pushes all permutations of "word" to vector str         
    if (!infile.eof())
    {
        string items;
        infile >> items;
        setwords.insert(items); //stores set of words from file
    }
    system("PAUSE");
    return 0;
}

现在我需要比较文件中的单词和存储在向量str中的排列把真正的单词打印出来。

我知道我需要使用set类的find方法。我只是不知道该怎么做。我也尝试过类似的东西,但没有运气,但我的思维过程可能是错误的。

for (unsigned int i = 0; i < str.size(); i++)
    if (setwords.find(word) == str[i])
        cout << str[i] << endl;

如果你们能帮助我或给我指出正确的方向,我将不胜感激。

首先,我想说这是一个很好的问题。我感谢新用户花时间详细阐述他们的问题。

问题是std::set<>find()方法返回一个指向它找到的值的迭代器对象,如果不能,则返回容器的end()。当你将它与str[i](一个字符串)比较时,它找不到一个合适的operator==()重载,既接受迭代器又接受字符串。

您可以将返回值与end()进行比较,以确定它是否找到了字符串,而不是与字符串进行完全比较:

if (setwords.find(str[i]) != setwords.end())
//                ^^^^^^     ^^^^^^^^^^^^^^

如果表达式返回true,那么它成功地在集合中找到了字符串。

还有一个潜在的问题,我想在你的代码中解决。使用if (!file.eof())是限制输入的错误方式。相反,您应该将提取作为条件的一部分,如下所示:

for (std::string item; infile >> item; )
{
    setwords.insert(item);
}
这里有另一种方法,使用std::istream_iterator<>:
setwords.insert(std::istream_iterator<std::string>(infile),
                std::istream_iterator<std::string>());

你实际上真的接近正确。

set::find方法不返回在集合中找到的值,而是返回一个指向该值的迭代器对象。因此,您的if语句将当前字符串与返回的迭代器对象进行比较,而不是与迭代器指向的值进行比较。

要获得迭代器所指向的值,只需像对指针那样对其解引用,并在其前面加上星号。这意味着您可能希望您的if语句看起来像这样:

if (*(setwords.find(word)) == str[i])

对于在集合中找到值的情况可以工作,但是对于没有找到值的情况可能会有问题。如果没有找到该值,则返回指向集合中最后一项之后位置的迭代器,并且不应该尝试解引用这样的迭代器(因为它没有指向有效对象)。

执行这些检查的方法通常是将返回的迭代器与指向集合末尾的迭代器进行比较(例如,本例中为set::end)。如果迭代器不匹配,则表示找到了项。
if (setwords.find(word) != setwords.end())
    cout << word << endl;

我认为你应该这样写:

for (unsigned int i = 0; i < str.size(); i++)
    if (setwords.find(str[i]) != setwords.end())
        cout << str[i] << endl;

但是我认为你不需要存储所有的排列。您可以存储一组按字母排序的单词。并与排序后的单词.....

进行比较

这里有一个更简单的解决方案

#include <iostream>                                                                                 
#include <fstream>                                                                                  
#include <string>                                                                                   
#include <locale>                                                                                   
#include <vector>                                                                                   
#include <algorithm>                                                                                
#include <map>                                                                                      
using namespace std;                                                                                
int main(int argc, char* argv[])                                                                    
{                                                                                                   
    map<string, string> mapwords;                                                                   
    ifstream infile;                                                                                
    infile.open("words.txt"); //reads file "words.txt"                                              
    string word = argv[1]; // input from command line                                               
    transform(word.begin(), word.end(), word.begin(), tolower); // transforms word to lower case.   
    sort(word.begin(), word.end()); // sorts the word                                               
    if (!infile.eof())                                                                              
    {                                                                                               
        string item;                                                                                
        infile >> item;                                                                             
        string sorted_item = item;                                                                  
        sort(sorted_item.begin(), sorted_item.end()); // sorts the word                             
        mapwords.insert(make_pair(sorted_item, item)); //stores set of words from file              
    }                                                                                               
    map<string, string>::iterator i = mapwords.find(word);                                          
    if(i != mapwords.end())                                                                         
        cout << i->second << endl;
    system("PAUSE");                                                                  
    return 0;                                                                                       
}   
相关文章:
  • 没有找到相关文章