2013-08-23 50 views
1

我有串std::string str(s);和S有一個像 s="one two three one one two..."創建使用unordered_map得到詞次數

單詞的列表,而且我想在與最大的出現值端字每個字的和發生哈希函數。

我宣佈發生類型:

typedef std::unordered_map<std::string> occurrences; 
occurrences s1; 

,我想的s內容分配到s1,我該怎麼辦呢?

,這裏以後就是代碼來獲得的,其具有一定的錯誤每個單詞出現:

for (std::unordered_map<std::string, int>::iterator it = s1.begin(); 
                it != s1.end(); 
                ++it) 
    { 
     std::cout << "word :" << it->first << "occured " << it->second << " times \n"; 
    } 

任何一個可以告訴我怎樣才能讓每一個字「一」的次數,「二」嗎?

根據要求在這裏,我將原來的代碼:

#include <string> 
#include <iostream> 
#include <unordered_map> 

int main() 
{ 
    typedef std::unordered_map<std::string,int> occurrences; 
    occurrences s1; 
    s1.insert(std::pair<std::string,int>("Hello",1)); 
    s1.insert(std::pair<std::string,int>("Hellos",2)); 

    for (std::unordered_map<std::string, int>::iterator it = s1.begin();it != s1.end();++it) 
    { 
     std::cout << "word :" << it->first << "occured " << it->second << " times \n"; 
    } 

    return 0; 
} 

改進代碼:

#include <iostream> 
#include <string> 
#include <sstream> 
#include <vector> 
#include <unordered_map> 

int main() 
{ 
    typedef std::unordered_map<std::string,int> occurrences; 
    occurrences s1; 

    // the string we're splitting. 
    std::string s = "one two three one one two"; 

    int maxcount=0,temp=0; 
    std::vector<std::string> vestring; 

    // create an input string stream 
    std::istringstream iss(std::move(s)); 

    // now simply extract strings until you reach end-of-file. 
    while (iss >> s) 
    { 
     temp=++s1[s]; 
    if(temp>=maxcount) 
    { 
     maxcount=temp; 
     vestring.push_back(s); 
    } 

    } 
    for (occurrences::const_iterator it = s1.cbegin();it != s1.cend(); ++it) 
     std::cout << it->first << " : " << it->second << std::endl;; 

    return 0; 
} 
+0

您是否熟悉['std :: istringstream <>'](http://en.cppreference.com/w/cpp/io/basic_istringstream)類?你也可以找到['std :: istream_iterator <>'](http://en.cppreference.com/w/cpp/iterator/istream_iterator)有幫助。 – WhozCraig

+0

@WhozCraig:我不知道。我讀過它,但沒有得到如何使用它在我的情況! – user123

回答

2

你有一個良好的開端。而且你的櫃檯使用的是正確的課程,這比大多數人都要多。你需要的機制是能夠從一個更大的字符串中解析出子字符串,而空白字符就是你的分隔符。 A std::istringstream<>將很好地做到這一點。

樣品

#include <iostream> 
#include <string> 
#include <vector> 
#include <sstream> 
#include <unordered_map> 

int main() 
{ 
    typedef std::unordered_map<std::string,int> occurrences; 
    occurrences s1; 

    // the string we're splitting. 
    std::string s = "one two two three one one two"; 

    // create an input string stream. we use std::move() to give 
    // the implementation the best chance at simply reassigning 
    // the buffer from the string to the stream 
    std::istringstream iss(std::move(s)); 

    // this will hold all occurrances of the strings with 
    // maximum count. each time a new maximum is found 
    // we clear the vector and push the new leader in. 
    std::vector<std::string> most; 
    int max_count = 0; 

    // now simply extract strings until you reach end-of-file. 
    while (iss >> s) 
    { 
     int tmp = ++s1[s]; 
     if (tmp == max_count) 
     { 
      most.push_back(s); 
     } 
     else if (tmp > max_count) 
     { 
      max_count = tmp; 
      most.clear(); 
      most.push_back(s); 
     } 
    } 

    // send our map to stdout. 
    for (occurrences::const_iterator it = s1.cbegin();it != s1.cend(); ++it) 
     std::cout << it->first << " : " << it->second << std::endl;; 

    // send the strings with max_count to stdout 
    std::cout << std::endl << "Maximum Occurrences" << std::endl; 
    for (std::vector<std::string>::const_iterator it = most.cbegin(); it != most.cend(); ++it) 
     std::cout << *it << std::endl; 

    return 0; 
} 

輸出

three : 1 
two : 3 
one : 3 

Maximum Occurrences 
one 
two 

這應該讓你開始。找到最大的數量對你來說應該沒有問題(提示:在每次插入之後,你的地圖都有當前剛剛處理的單詞數)。

還有更有效的方法來做到這一點,但對於你現在正在做的事情來說,它可能還不夠。

+0

謝謝,但是花了那麼多時間來編譯?你可以請求解釋我三行'std :: istringstream iss(std :: move(s)); while(iss >> s) ++ s1 [s];'它在這裏有什麼功能?我展示教程,但我很清楚! – user123

+0

我也無法得到最大發生的詞的邏輯! – user123

+0

@卡里姆漢那麼,你爲每項工作積累了一個計數器擴展while循環來檢查它剛剛遞增的計數值大於你在循環之外保留的'maxcount'值。如果是,則將該字符串保存在循環外的字符串var中,並將'maxcount'更新爲新的最大值。當你的循環完成時,它會有最高的字符串發生(除非有聯繫,在這種情況下,它將有* first *到達該值,或者最後一個如果使用'> =')。 – WhozCraig