2013-02-24 119 views
0

我需要查找字符串數組中出現次數最多的元素。我不知道該怎麼做,因爲我沒有太多的經驗。我不知道指針/哈希表。字符串數組中出現次數最多的元素

我已經完成了整數,但我不能讓它適用於字符串。

我的版本:

#include <iostream> 
using namespace std; 
int main(int argc, char *argv[]) 
{ 
    int a[]={1,2,3,4,4,4,5}; 
    int n = sizeof(a)/sizeof(int); 
    int *b=new int [n]; 
    fill_n(b,n,0); // Put n times 0 in b 

    int val=0; // Value that is the most frequent 
    for (int i=0;i<n;i++) 
     if(++b[a[i]] >= b[val]) 
      val = a[i]; 

    cout<<val<<endl; 
    delete[] b; 
    return 0; 
    } 

查找字符串數組中最頻繁出現的元素任何幫助表示讚賞!

+1

它可能看起來幾乎相同的方式,但你一直在使用'std :: map '而不是'int * b'。 – LihO 2013-02-24 19:52:52

+0

呃..我試過地圖,沒有真正使它工作,我一直在谷歌搜索一小時的提示,沒有設法找到有用的東西... – 2013-02-24 20:02:36

+0

你爲什麼嘗試刺? – 2013-02-24 20:04:43

回答

0

你可以考慮使用vector字符串和使用一個版本兼容0計數事件:

#include <iostream> 
#include <vector> 
#include <map> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    vector<string> a; 
    map<string, int> m; 

    // fill a with strings 
    a.push_back("a"); 
    a.push_back("b"); 
    a.push_back("b"); 
    a.push_back("c"); 
    a.push_back("c"); 
    a.push_back("c"); 
    a.push_back("d"); 
    a.push_back("e"); 

    // count occurrences of every string 
    for (int i = 0; i < a.size(); i++) 
    { 
     map<string, int>::iterator it = m.find(a[i]); 

     if (it == m.end()) 
     m.insert(pair<string, int>(a[i], 1)); 

     else 
     m[a[i]] += 1; 
    } 

    // find the max 
    map<string, int>::iterator it = m.begin(); 
    for (map<string, int>::iterator it2 = m.begin(); it2 != m.end(); ++it2) 
    { 
     if (it2 -> second > it -> second) 
     it = it2; 
    } 

    cout << it -> first << endl; 
    return 0; 
} 

這種解決方案可能不是優雅和效率方面最好的一個,但它應該給的想法。

+0

非常感謝。我會看看我能用這個做什麼。只是一個簡單的問題,爲什麼你在int main()中使用參數「argc」? – 2013-02-24 20:40:53

+0

好吧,實際上沒有什麼。我只是習慣寫它們,但在這種情況下'int main(int argc,char * argv [])'可以用'int main()'代替,程序仍然可以工作。 – 2013-02-24 20:45:43

+0

是的,我知道它的工作原理,但我見過很多人使用它,我不知道爲什麼。無論如何,我有一個小問題,你如何手動輸入元素通過CIN的矢量,而不是做push_back?我試過爲push_back(i)做一個for循環,但是效果不好。 – 2013-02-24 20:49:22

1

首先,考慮使用C++容器而不是普通數組。 (如果需要,搜索「數組到矢量」或它們以在它們之間進行轉換)。

然後,如果您可以使用C++ 11,可以這樣做(沒有C++ 11,它會變成有點冗長,但仍是可行的):

std::string most_occurred(const std::vector<std::string> &vec) { 
    std::map<std::string,unsigned long> str_map; 
    for (const auto &str : vec) 
    ++str_map[str]; 

    typedef decltype(std::pair<std::string,unsigned long>()) pair_type; 

    auto comp = [](const pair_type &pair1, const pair_type &pair2) -> bool { 
    return pair1.second < pair2.second; }; 
    return std::max_element(str_map.cbegin(), str_map.cend(), comp)->first; 
} 

這是與舊的C++

bool comp(const std::pair<std::string,unsigned long> &pair1, 
      const std::pair<std::string,unsigned long> &pair2) { 
    return pair1.second < pair2.second; 
} 

std::string most_occurred(const std::vector<std::string> &vec) { 
    std::map<std::string,unsigned long> str_map; 
    for (std::vector<std::string>::const_iterator it = vec.begin(); 
     it != vec.end(); ++it) 
    ++str_map[*it]; 
    return std::max_element(str_map.begin(), str_map.end(), comp)->first; 
} 
+0

不幸的是,我使用的是C++ 96或其它任何東西(「正常」的)。我並不真正瞭解C++ 11,幾乎沒有任何關於它的信息(除了我聽說過它) 。 – 2013-02-24 20:16:47

+0

@JohnSmith所有現代的C++程序員都應該知道C++ 11,它只是現代C++。但是我仍然發佈了與舊版C++兼容的另一個版本。 – user2015453 2013-02-24 20:28:04

+0

我剛剛開始一個月前,所以我很懷疑我很快就會學習C++ 11。感謝您轉換它。 – 2013-02-24 20:42:22

相關問題