gpt4 book ai didi

c++ - 如何使用STL中的容器读取文件

转载 作者:行者123 更新时间:2023-11-30 03:23:40 26 4
gpt4 key购买 nike

我必须从 "testdata.txt" 中逐字阅读,然后在另一个文件 "dictionary.txt" 中查找相同的单词。我已经在 ReadDictionary() 函数中实现了读取 "dictionary.txt" 的代码。但我必须实现 ReadTextFile() 公共(public)成员函数才能将名为 “testdata.txt” 的文件读入“KnownWords”和“UnknownWords”数据中是私有(private)成员。只有“已知”的单词应该放在“KnownWords”中,其余的放在“UnknownWords”中。我必须使用 map 和 pair 但我不知道如何在我的编程中使用它。有人可以帮我弄清楚这个以获得这个输出:

89 known words read.
49 unknown words read.

int main():

WordStats ws;
ws.ReadTxtFile();

头文件:

using namespace std;
typedef map<string, vector<int> > WordMap;
typedef WordMap::iterator WordMapIter;

class WordStats
{
public:
WordStats();
void ReadDictionary();
void DisplayDictionary();
void ReadTxtFile();
private:
WordMap KnownWords;
WordMap UnknownWords;
set<string> Dictionary;
char Filename[256];
};

这是我的程序:

WordStats::WordStats(){
strcpy(Filename,"testdata.txt");
}

// Reads dictionary.txt into Dictionary
void WordStats::ReadDictionary(){
string word;
ifstream infile("dictionary.txt");
if(!infile)
{
cerr << "Error Opening file 'dictionary.txt. " <<endl;
exit(1);
}
while(getline(infile,word))
{
transform (word.begin(), word.end(), word.begin(), ::tolower);
Dictionary.insert(word);
}
infile.close();
cout << endl;
cout << Dictionary.size() << " words read from dictionary. \n" <<endl;

}
// Reads textfile into KnownWords and UnknownWords
void WordStats::ReadTxtFile(){
string words;
vector<string> findword;
vector<int> count;
ifstream ifile(Filename);
if(!ifile)
{
cerr << "Error Opening file 'dictionary.txt. " <<endl;
exit(1);
}
while(!ifile.eof())
{
getline(ifile,words);
//KnownWords.insert( pair<string,int>( KnownWords, words ) );
findword.push_back(words);
Paragraph = KnownWords.find(words);
//stuck here
}
}

最佳答案

您似乎需要检查 Dictionary 以查看它是否包含您阅读的每个单词,然后选择要修改的 KnownWordsUnknownWords

void WordStats::ReadTxtFile(){
std::ifstream ifile(Filename);
if(!ifile)
{
std::cerr << "Error Opening file " << Filename << std::endl;
exit(1);
}

我已经清理了你的局部声明,所以变量只存在了尽可能短的时间。

假设文件中包含由空格和换行符分隔的单词,读取每个单词

    for (std::string word; ifile >> word; )
{

小写

        transform (word.begin(), word.end(), word.begin(), ::tolower);

然后查看它是否在Dictionary

        if (Dictionary.count(word))
{

记录在 KnownWords[word] 中的位置。

            KnownWords[word].push_back(ifile.tellg());
}
else
{

或者在 UnknownWords[word] 中。

            UnknownWords[word].push_back(ifile.tellg()); 
}
}

然后显示这些中的大小以获得所需的输出。

    std::cout << KnownWords.size() << " known words read." << std::endl;
std::cout << UnknownWords.size() << " unknown words read." << std::endl;
}

您可以用条件表达式替换复制操作的条件语句。注意 Words

声明中的引用类型
WordMap & Words = (Dictionary.count(word) ? KnownWords : UnknownWords);
Words[word].push_back(ifile.tellg());

作为一个完整的函数:

void WordStats::ReadTxtFile(){
std::ifstream ifile(Filename);
if(!ifile)
{
std::cerr << "Error Opening file " << Filename << std::endl;
exit(1);
}

for (std::string word; ifile >> word; )
{
transform (word.begin(), word.end(), word.begin(), ::tolower);
WordMap & Words = (Dictionary.count(word) ? KnownWords : UnknownWords);
Words[word].push_back(ifile.tellg());
}

std::cout << KnownWords.size() << " known words read." << std::endl;
std::cout << UnknownWords.size() << " unknown words read." << std::endl;
}

关于c++ - 如何使用STL中的容器读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50229499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com