자, 저는지도 사용법을 스스로 가르쳐려고합니다. 내 의도는 txt 파일을 열고 그 단어를 모두 집계 한 다음 특정 단어가 몇 번 나왔는지 보여주는 것입니다. 그런 다음 (가능한 경우) 두 번째 맵에서 첫 번째 맵을 사용하여 해당 값을 호출하고 자주 발생하는 상위 10 개 (또는 20 개 또는 그와 같은) 빈번한 단어 만 출력하고 횟수를 인쇄합니다 (최대에서 최소까지) 그것은 실제 단어와 함께 발생합니다.C++ : 맵을 사용하여 문자열 파일의 문자열 값 정렬 및 인쇄
나는 이미 모든 단어를 출력하는 방법과 발생 횟수를 알아 냈습니다. 그리고 나는 그지도가 매우 멋지다는 것을 이미 자동으로 그 안에서 호출 한 실제 문자열을 정렬한다고 생각합니다. 내 문제는 문자열이 아닌 정렬 된 값이 필요하다는 것입니다.
코드의 특정 기능에 대해 의견을 말했지만이 다른지도에 대해서는 잘 모르겠습니다.
다른 아이디어 만 찾고 있습니다. 제발 그러지 마세요.
** 누군가 나에게 priority_queue에 대해 언급했지만 그 역시 나에게 새롭다. 내가 이해할 수 있도록 예를 들어 좀 더 구두로 설명 할 수 있다면, 좋을 것입니다 !!
std:map<int,string> count_word
에 따라이에 대한 기존의 맵에서 쌍을 삽입 할
#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");
}
나는이 접근법이 당신의 삶을 복잡하게 만든다고 생각합니다. 당신은지도 int -> string을 가질 수 있었지만, 어떻게 넥타이를 다룰 수 있습니까? 두 단어의 숫자가 같으면 서로 재정의합니다. 당신은 map>을 가지고 세트에 추가 할 수 있습니다. 그러나 코드가 더 복잡해질 것입니다. (넥타이가 있기 때문에 같은 주파수의 11 단어가있을 수 있기 때문에 꼭 필요하지 않습니다. 10). 우선 순위 큐가 의미가 있거나 의 벡터이고 freq로 정렬 ... –
okaram