我正在爲一所學校的項目工作,但我堅持我認爲只是一小部分,但我無法弄清楚。C語言中的Anagram求解器
這是我到目前爲止有:
#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;
如果你們可以幫助或指引我在正確的方向,我將不勝感激。
+1用於付出努力並張貼您被卡住的地方,而不只是要求我們爲您編寫代碼。這很不尋常。 –