C ++:使用map对文本文件中字符串出现的值进行排序和打印

时间:2014-12-05 00:55:09

标签: c++ sorting map priority-queue

好的伙计们,我正在努力教自己如何使用地图。我的意图是打开一个txt文件并计算所有这些单词,然后显示特定单词出现的次数。然后(如果它可能)我想在第二个映射中使用第一个映射来调用这些值并仅输出发生的前10个(或20个或其他)频繁单词并打印次数(从最大到最小)它会与实际的单词一起出现。

我已经想出如何输出所有单词以及它们出现的次数。而且我觉得很酷的地图已经对我在其中自动调用的实际字符串进行了排序。我的问题只是我需要对这些值进行排序,而不是字符串。

我已经对代码的具体功能发表了评论,但我不确定这个其他地图。

我只是在寻求不同的想法。请不要吝啬。

**有人向我提到过priority_queue,但这对我来说也是新的。如果你能用一个更加口头的方式解释这一点,以便我能理解,那就太棒!!

#include <iostream>
#include <map>
#include <fstream>
#include <string>

using namespace std;

//makes word count a declaration 
//makes count word a declaration 
typedef map <string, int> word_count;
typedef map <int, string> count_word; 


int main()
{
word_count word_count;
string filename;


// Get the filename.
cout << "enter data.txt ";
cin >> filename;

// Open file.
ifstream file(filename.c_str());

// Read in all the words.
string word;
while (file >> word)
{
    // Remove punctuation.
    int index;
    while ((index = word.find_first_of(".,!?\\;-*+[]<>() '")) != string::npos)
    {
        word.erase(index, 1);
    }

    ++word_count[word];
 }


 std::map <int, string> count_word;

 // Print out the first 10 words counts.
 word_count::const_iterator current(word_count.begin());


 int count = 0;
 while (current != word_count.end() && count<10)
 {

    count++;
    cout << "The word '" << current->first << "'      appears " << current->second << " times" << endl;
    count_word.insert(std::pair<int, string>(current->second, current->first));
    ++current;

 }


 count_word::const_iterator new_current(count_word.begin());
 count = 0;

while (new_current != count_word.end() && count<10)
{

     count++;
    cout << new_current -> first <<  " times    appears the word '" <<
            current -> second <<  endl;
     ++new_current;
}

 system("pause");
  }

2 个答案:

答案 0 :(得分:0)

您可以按照以下方式创建新的地图容器

std:map<int,string> count_word

并将现有地图中的对插入此对。新地图将根据您的需要自行排序。

这里是代码片段。我没有编译。

std:map<int,string> count_word;
word_count::const_iterator current(word_count.begin());


int count = 0;
while (current != word_count.end() && count<10)
{

    count++;
    cout << "The word '" << current -> first << "'      appears " << current -> second << " times" << endl;
    count_word.insert(std::pair<int,string>(current->second,current->first);
    ++current;

}

count_word::const_iterator new_current(count_word.begin());
//for(auto &x:count_word)
//std::cout<<x->first<<"no of times"<< x->second << "word"<<endl;
//Either you can use above 2 line to print or below given few lines 
while (new_current != word_count.end() && count<10)
{

     count++;
    cout << new_current -> first <<  " times    appears the word '" <<<< current -> second <<  endl;
     ++new_current;
}

答案 1 :(得分:0)

priority queue允许设置一个自定义比较器,您可以利用它,通过比较您的队列的计数(映射值)可以按值排序(map也可以使用比较器但它只适用于键):

typedef pair<string,int> str_to_int; // = word_count::value_type
struct Compare {
    bool operator()(const str_to_int & a, const str_to_int & b) {
        return a.second < b.second;
    }
};
// ...
priority_queue<str_to_int, vector<str_to_int>, Compare> queue(word_count.begin(), word_count.end());
// Print the top 10
for (int i=0; i<10; ++i) {
    const str_to_int & e = queue.top();
    queue.pop();
    cout << "The word '" << e.first << "'      appears " << e.second << " times" << endl;
}