【问题标题】:How to print words using reverse_iterator in STL如何在 STL 中使用 reverse_iterator 打印单词
【发布时间】:2018-10-19 12:17:34
【问题描述】:

我必须阅读两个文本文件,然后将第二个文件中的单词与第一个文件进行比较。然后,我必须显示KnownWords,它们是两个文件中相同的单词,其余不同的单词是UnknownWords。下一步是,我必须在DisplayMostFreqKnownWords() 中显示最常见的已知词,在DisplayMostFreqUnknownWords() 函数中显示未知词。我已经成功完成DisplayMostFreqKnownWords(),到目前为止输出还不错。我将相同的代码从DisplayMostFreqKnownWords() 复制到DisplayMostFreqUnknownWords(),但在这个函数中它没有在输出中显示任何内容。我不知道出了什么问题。有人能解决这个问题吗?

输出是:

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];
}

我的程序:

// 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;
}
}

【问题讨论】:

  • 不清楚你在问什么。请edit您的帖子澄清并提供minimal reproducible example
  • 代码使用for(; count&lt;=10作为循环条件,但从不使用count = 0;设置起始值。
  • 1) 你从不初始化count 2) 你应该检查UnknownWords 的内容,看看你是否真的在displayFreqUnknownWords 中放入了元素
  • 如果这些是错误,那么为什么 DisplayFreqKnownWords() 工作得非常好,而两者的编码几乎相似。另外,如何检查 UnknownWords 以了解我是否真的将元素放在 displayFreqUnknownWords 中。

标签: c++ vector stl multimap reverse-iterator


【解决方案1】:

这是一种表达我认为是您的用例的方法。我使用过 c++17 元组扩展。

我已经使用unordered_map 来推断哪些词是已知的或未知的,并使用两个multimaps 来确定已知和未知的词频。

希望对你有帮助。

#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';
    }
}

【讨论】:

    猜你喜欢
    • 2022-07-28
    • 2013-07-26
    • 1970-01-01
    • 2017-08-01
    • 2010-09-29
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多