2014-12-05 3 views
0

자, 저는지도 사용법을 스스로 가르쳐려고합니다. 내 의도는 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"); 
    } 
+0

나는이 접근법이 당신의 삶을 복잡하게 만든다고 생각합니다. 당신은지도 int -> string을 가질 수 있었지만, 어떻게 넥타이를 다룰 수 있습니까? 두 단어의 숫자가 같으면 서로 재정의합니다. 당신은 map >을 가지고 세트에 추가 할 수 있습니다. 그러나 코드가 더 복잡해질 것입니다. (넥타이가 있기 때문에 같은 주파수의 11 단어가있을 수 있기 때문에 꼭 필요하지 않습니다. 10). 우선 순위 큐가 의미가 있거나 의 벡터이고 freq로 정렬 ... – okaram

답변

0

당신은 새로운 맵 컨테이너를 만들 수 있습니다. 새지도는 원하는대로 정렬됩니다.

여기 코드 스 니펫. 컴파일하지 않았습니다.

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; 
} 
+0

논리적으로 보겠습니다.이 맵은 선언 된 후에 값으로 정렬하는 방법을 정확히 모릅니다. –

+0

@RishabhPatel 코드 스 니펫 – Steephen

+0

을 추가했습니다. 그래서 두 개의 다른 while 문을 사용하여 스 니펫을 두 번 컴파일하고 그 단어만을 정렬했습니다. 지도가 자동으로이를 이해합니다. 해당 단어의 값을 정렬 할 수 있다면 어떨까요?


예 "Zeta"가 "18"번 표시됩니다.
"Tau"가 "12"번 표시됩니다.
"감마"가 "9"번 표시됩니다. –

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