【发布时间】:2014-05-09 09:55:25
【问题描述】:
我正在为学校做一个项目,我被困在我认为只是一小部分的事情上,但我无法弄清楚。
这是我目前所拥有的:
#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 表示努力并在您遇到困难的地方发布,而不是仅仅要求我们为您编写代码。它变得不寻常。