该程序查找文字并删除在文本文件中重复的所有文字。在这里,我通过输入一个特定的单词来编写这段代码,但是我希望程序能够自己找到那种单词,结果只会显示未重复的单词。我已经尝试过最好但失败了。 你能建议我找一个更好的方法吗?
int main()
{
ifstream fin("name.txt");
ofstream fout("result.txt");
string word;
string line;
int pos;
int ccount=0;;
cout<<"Please input word:";
cin>>word;
while(getline(fin,line))
{
pos= 0;
while(line.find(word,pos)!=string::npos)
{
pos = line.find(word,pos);
line.erase(pos,word.length());
}
fout<<line<<endl;
}
fin.close();
fout.close();
}
答案 0 :(得分:1)
您可以使用std::vector
在阅读时跟踪单词以及std::set
,以确保只将其添加到std::vector
一次。您希望std::vector
的原因是因为std::set
不会按插入顺序保留std::string
。这应该做的工作:
#include <algorithm>
#include <fstream>
#include <unordered_set>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> words;
std::unordered_set<std::string> uniqueWords;
std::ifstream fin("in.txt");
if (fin.is_open())
{
std::string line;
while (getline(fin, line))
{
std::istringstream iss(line);
for (std::string word; iss >> word;)
{
std::string lowercaseWord = word;
std::transform(word.begin(), word.end(), lowercaseWord.begin(), ::tolower);
if (uniqueWords.find(lowercaseWord) == uniqueWords.end())
{
uniqueWords.insert(lowercaseWord);
words.push_back(word);
}
}
}
fin.close();
}
std::ofstream fout("out.txt");
if (fout.is_open())
{
for each (std::string word in words)
{
fout << word << " ";
}
fout.close();
}
return 0;
}
答案 1 :(得分:0)
好的,只能使用相当复杂的工具:只需要语言环境即可将所有单词转换为小写并处理标点符号。
规格:
这是一个可能的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <locale>
int main() {
std::ifstream in("name.txt"); // io streams
std::ofstream out("result.txt")
std::set<std::string> uniq_words; // a set to store unique words
// ctyp facet of default locale to identify punctuations
std::locale loc;
const std::ctype<char>& ctyp = std::use_facet<std::ctype<char> >(loc);
std::string word;
while (in >> word) { // split text in words
// ignore any punctuation at the beginning or the end of the word
auto beg = word.begin();
while ((beg != word.end()) && ctyp.is(ctyp.punct, *beg)) beg++;
auto end = word.end();
while ((end > beg) && ctyp.is(std::ctype<char>::punct, end[-1])) end--;
for (auto it=beg; it != end; it++) { // convert what is kept to lower case
*it = ctyp.tolower(*it);
}
uniq_words.insert(std::string(beg, end)); // insert the lower case word in the set
}
// Ok, we have all unique words: write them to output file
for (auto w: uniq_words) {
out << w << " ";
}
out << std::endl;
return 0;
}