C++ 如何在STL中使用反向迭代器打印单词
我必须阅读两个文本文件,然后比较第二个文件和第一个文件中的单词。然后,我必须显示两个文件中相同的C++ 如何在STL中使用反向迭代器打印单词,c++,vector,stl,multimap,reverse-iterator,C++,Vector,Stl,Multimap,Reverse Iterator,我必须阅读两个文本文件,然后比较第二个文件和第一个文件中的单词。然后,我必须显示两个文件中相同的已知词,其余不相同的词是未知词。下一步是,我必须在DisplayMostFreqKnownWords()函数中显示最常见的已知单词,并在DisplayMostFreqKnownWords()函数中显示未知单词。我已经成功地完成了DisplayMostFreqKnownWords(),到目前为止输出正常。我将相同的代码从DisplayMostFreqKnownWords()复制到DisplayMostF
已知词
,其余不相同的词是未知词
。下一步是,我必须在DisplayMostFreqKnownWords()
函数中显示最常见的已知单词,并在DisplayMostFreqKnownWords()
函数中显示未知单词。我已经成功地完成了DisplayMostFreqKnownWords()
,到目前为止输出正常。我将相同的代码从DisplayMostFreqKnownWords()
复制到DisplayMostFreqKnownWords()
中,但在这个函数中,它没有在输出中显示任何内容。我不知道怎么了。有人能想出这个吗
输出为:
Displaying most frequent known words
Word Count
the 19
a 14
of 11
artificial 11
that 10
to 7
signal 7
and 7
in 6
they 5
Displaying most frequent unknown words
Word Count
头文件:
typedef map<string, vector<int> > WordMap;
typedef WordMap::iterator WordMapIter;
class WordStats
{
public:
WordStats();
void ReadDictionary();
void DisplayDictionary();
void ReadTxtFile();
void DisplayKnownWordStats();
void DisplayUnknownWordStats();
void DisplayMostFreqKnownWords();
void DisplayMostFreqUnknownWords();
private:
WordMap KnownWords;
WordMap UnknownWords;
WordMapIter Paragraph;
set<string> Dictionary;
char Filename[256];
}
typedef映射WordMap;
typedef WordMap::迭代器WordMapIter;
类WordStats
{
公众:
WordStats();
void ReadDictionary();
void DisplayDictionary();
void ReadTxtFile();
void displaynwordstats();
void DisplayUnknownWordStats();
void DisplayMostFreqKnownWords();
void DisplayMostFreqUnknownWords();
私人:
单词地图;
单词映射未知词;
文字映射器段;
设置字典;
字符文件名[256];
}
我的节目:
// Displays 10 most frequent words in KnownWords
void WordStats::DisplayMostFreqKnownWords(){
int count;
multimap<int,string > displayFreqWords;// new map with int as key
(multimap because key could occur more than once)
multimap<int,string >::reverse_iterator rit = displayFreqWords.rbegin();
for (Paragraph = KnownWords.begin(); Paragraph != KnownWords.end();
++Paragraph){ // iterate map again
string word = (*Paragraph).first;
int cnt = (*Paragraph).second.size();
displayFreqWords.insert(pair<int,string>(cnt,word));
}
// multimap<int,string>::iterator rit; // iterator for new map
cout <<" Word Count\n";
for(; count<=10 && rit!=displayFreqWords.rend(); rit++, ++count){
string word = (*rit).second;
int cnt = (*rit).first;
cout << setw(15) << word << setw(10) << cnt << endl;
}
}
// Displays 10 most frequent words in UnknownWords
void WordStats::DisplayMostFreqUnknownWords(){
int count;
multimap<int,string > displayFreqUnknownWords;
multimap<int,string >::reverse_iterator rrit =
displayFreqUnknownWords.rbegin();
for (Paragraph = UnknownWords.begin(); Paragraph !=
UnknownWords.end(); ++Paragraph){
string word = (*Paragraph).first;
int cnt = (*Paragraph).second.size();
displayFreqUnknownWords.insert(pair<int,string>(cnt,word));
}
// multimap<int,string>::iterator rit; // iterator for new map
cout <<" Word Count\n";
for(; count<=10 && rrit!=displayFreqUnknownWords.rend(); rrit++, ++count){
string wrd = (*rrit).second;
int ccnt = (*rrit).first;
cout << setw(15) << wrd << setw(10) << ccnt << endl;
}
}
//显示已知单词中最常用的10个单词
void WordStats::DisplayMostFreqKnownWords(){
整数计数;
multimap displayFreqWords;//以int为键的新映射
(多重映射,因为键可能出现多次)
multimap::reverse_iterator rit=displayFreqWords.rbegin();
for(段落=knowwords.begin();段落!=knowwords.end();
++段落){//再次迭代映射
字符串字=(*段)。首先;
int cnt=(*段落).second.size();
displayFreqWords.insert(成对(cnt,word));
}
//multimap::iterator rit;//新映射的迭代器
cout这里有一种表达我认为是您的用例的方法。我使用了c++17元组扩展
我使用了unordered_-map
来推断哪些单词是已知的或未知的,使用两个multimap
s来确定已知和未知的单词频率
希望对你有帮助
#include <sstream>
#include <tuple>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <iterator>
#include <map>
#include <iostream>
#include <iomanip>
#include <fstream>
// Set this to 1 to run a static test
#define TESTING 0
#if TESTING
using input_type = std::istringstream;
std::tuple<input_type, input_type> open_inputs() {
return {
std::istringstream("the big black cat sat on the grey mat"),
std::istringstream("the gold small cat lay on the purple mat")
};
}
#else
using input_type = std::ifstream;
std::tuple<input_type, input_type> open_inputs() {
return {
std::ifstream("left_file.txt"),
std::ifstream("right_file.txt"),
};
}
#endif
struct Counts {
int left_count = 0, right_count = 0;
int total() const {
return left_count + right_count;
}
bool is_known() const {
return left_count && right_count;
}
};
template<class F>
void for_each_word_in_file(std::istream &is, F f) {
std::for_each(std::istream_iterator<std::string>(is),
std::istream_iterator<std::string>(),
f);
}
int main() {
// open files
auto[left, right] = open_inputs();
auto known_words = std::unordered_map<std::string, Counts>();
// count words in each file
for_each_word_in_file(left, [&known_words](auto &&word) {
++known_words[word].left_count;
});
for_each_word_in_file(right, [&known_words](auto &&word) {
++known_words[word].right_count;
});
// map counts to words, in descending order, allowing multiple entries of the same count
std::multimap<int, std::string, std::greater<>> known_ordered, unknown_ordered;
// iterate all words seen, putting into appropriate map
for (auto&&[word, counts] : known_words) {
(counts.is_known() ? known_ordered : unknown_ordered)
.emplace(counts.total(), word);
}
// emit results
std::cout << "Known words by frequency\n";
for (auto&&[freq, word] : known_ordered) {
std::cout << std::setw(15) << word << " " << freq << '\n';
}
std::cout << "\nUmknown words by frequency\n";
for (auto&&[freq, word] : unknown_ordered) {
std::cout << std::setw(15) << word << " " << freq << '\n';
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//将此设置为1以运行静态测试
#定义测试0
#如果测试
使用输入类型=std::istringstream;
std::元组打开_输入(){
返回{
istringstream(“坐在灰色垫子上的大黑猫”),
std::istringstream(“躺在紫色垫子上的金色小猫”)
};
}
#否则
使用输入类型=std::ifstream;
std::元组打开_输入(){
返回{
std::ifstream(“left_file.txt”),
std::ifstream(“right_file.txt”),
};
}
#恩迪夫
结构计数{
int left_count=0,right_count=0;
int total()常量{
返回左计数+右计数;
}
布尔是已知的常数{
返回左计数和右计数;
}
};
样板
文件(std::istream&is,F)中的每个单词都是空的{
std::for_each(std::istream_迭代器(is),
std::istream_迭代器(),
f) );
}
int main(){
//打开文件
自动[左,右]=打开_输入();
自动已知单词=std::无序单词映射();
//计算每个文件中的字数
对于_文件中的每个_单词(左,[&known_单词](自动和&word){
++已知单词[单词]。左计数;
});
对于_文件中的每个_单词(右,[&known_单词](auto&&word){
++已知单词[单词]。右\u计数;
});
//按降序将计数映射到单词,允许相同计数的多个条目
std::多映射已知有序,未知有序;
//迭代所有看到的单词,放入适当的地图中
for(自动&&[字数,计数]:已知字数){
(counts.is_known()?known_ordered:unknown_ordered)
.emplace(counts.total(),word);
}
//发出结果
std::难道你的问题不清楚吗。请你的帖子澄清并提供答案。代码使用表示(;count1)您从不初始化count
2)您应该检查UnknownWord
的内容,以查看是否确实将元素放入DisplayFreqUnknownWord
如果这些是错误,那么为什么显示FreqKnownWord()虽然两者的编码几乎相似,但工作非常好。另外,如何检查未知词以了解我是否真的将元素放入displayFreqUnknowwords中。