国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

統計單詞出現個數(STL-map求解)

2019-11-08 18:23:02
字體:
來源:轉載
供稿:網友

string strs[] = { “zhangsan”, “zhangsan”, “lisi”, “wangwu”, “lisi”, “zhaoliu”,”lisi” }; 問題一:統計單詞出現的次數 看到這個題,有很多思路可以解決,目前剛好在了解STLset/map中,所以就用map來解這個問題。 根據map的諸多借口可以有多種方法來解決這個問題,但是其實都是運用的map的特性。 覺得下面這些有必要了解一下: 首先map是關聯容器,之前set篇說過。map的元素都通過元素鍵值自動排序,擁有并且獨一無二的鍵值,因為map不允許兩個元素擁有相同的鍵值。map的元素都是pair類型。同時擁有鍵值(key)和實值(value)。pair的第一個元素是鍵值,第二個元素是實值。另外map的底層是由RB-tree為底層機制,因此map對插入,刪除,查找能保證log(N)的時間復雜度。對于海量的數據的插入和查詢,map是一個不錯的選擇。 map形式如下:

template < class Key, // map::key_type class T, // map::mapped_type class Compare = less<Key>, // map::key_compare 這里可以看到缺省采用的是遞增排序(升序) class Alloc = allocator<pair<const Key,T> > // map::allocator_type > class map;

map::insert()形式如下: 這里寫圖片描述

而pair的定義如下:

template<class TI,class T2>struct pair{ typedef TI first_type; typedef T2 second_type; T1 first; T2 second; pair():first_type(T1()),second_type(T2()){} pair(const T1& a,const T2& b):first(a),second(b){}}

可以將單詞當作鍵值,次數當作實值 方法一:插入單詞前先就行判斷,是否已經擁有,如果已經插入則直接給實值+1,如果沒有則進行插入操作。

string strs[] = { "zhangsan", "zhangsan", "lisi", "wangwu", "lisi", "zhaoliu","lisi" }; map<string, int> CountMap; for (size_t idx = 0; idx < sizeof(strs) / sizeof(strs[0]); ++idx) { //第一種 map<string, int>::iterator it = CountMap.find(strs[idx]); if (it != CountMap.end()) { it->second++; } else { CountMap.insert(pair<string, int>(strs[idx], 1)); } } 方法二:要知道如定義一個pair類型的第二個元素會返回一個false),則說明map中已經存在這個單詞,給實值+1就可以 string strs[] = { "zhangsan", "zhangsan", "lisi", "wangwu", "lisi", "zhaoliu","lisi" }; map<string, int> CountMap; pair<map<string, int>::iterator, bool> ret; for (size_t idx = 0; idx < sizeof(strs) / sizeof(strs[0]); ++idx) { //第二種 ret = CountMap.insert(pair<string, int>(strs[idx], 1)); if (ret.second == false) { ret.first->second++; } } 方法三:需要對map了解,直接使用庫里提供的[]運算符重載。通過鍵值找節點,直接給給實值+1.string strs[] = { "zhangsan", "zhangsan", "lisi", "wangwu", "lisi", "zhaoliu","lisi" }; map<string, int> CountMap; for (size_t idx = 0; idx < sizeof(strs) / sizeof(strs[0]); ++idx) { CountMap[strs[idx]]++; }

這里寫圖片描述 可以看到單詞的個數已經統計出來。第三種方法就充分說明熟悉STL庫的作用,想想這要是在筆試別人不需要多想直接就能得到答案,而我們還在思考應該怎么求(這也許就是差距啊) 問題二: 找出出現次數最多的三個單詞 首先想的是將所有單詞都插入到vector中,然后在用sort進行排序,但是仔細想想一個string類型就占很多字節,如果單詞稍微多點的話,豈不是太浪費空間了? 我們可以打破慣性思維,為什么非得把單詞存到vector中,為什么不把單詞所在節點的迭代器放到vector中呢?然后再通過自定義比較類型比較次數從而使用sort就行排序。

vector<map<string, int>::iterator> v; map<string, int>::iterator CountIt = CountMap.begin(); while (CountIt != CountMap.end()) { v.push_back(CountIt); ++CountIt; } struct Compare { bool Operator()(map<string, int>::iterator l, map<string, int>::iterator r) { return l->second > r->second; } }; sort(v.begin(),v.end(),Compare());

測試: 這里寫圖片描述 完整代碼入下:

#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<map>#include<vector>#include<algorithm>#include<set>#include<queue>using namespace std;int main(){ string strs[] = { "zhangsan", "zhangsan", "lisi", "wangwu", "lisi", "zhaoliu","lisi" }; map<string, int> CountMap; pair<map<string, int>::iterator, bool> ret; for (size_t idx = 0; idx < sizeof(strs) / sizeof(strs[0]); ++idx) { //第一種 /*map<string, int>::iterator it = CountMap.find(strs[idx]); if (it != CountMap.end()) { it->second++; } else { CountMap.insert(pair<string, int>(strs[idx], 1)); } */ //第二種 /*ret = CountMap.insert(pair<string, int>(strs[idx], 1)); if (ret.second == false) { ret.first->second++; }*/ //第三種 CountMap[strs[idx]]++; /* //operator[]定義: mapped_type& operator[](const key_type& _Keyval) { // find element matching _Keyval or insert with default mapped iterator _Where = this->lower_bound(_Keyval); if (_Where == this->end() || this->_Getcomp()(_Keyval, this->_Key(_Where._Mynode()))) _Where = this->emplace_hint(_Where, piecewise_construct, _STD forward_as_tuple(_Keyval), tuple<>()); return (_Where->second); }*/ } //用sort排序 vector<map<string, int>::iterator> v; map<string, int>::iterator CountIt = CountMap.begin(); while (CountIt != CountMap.end()) { v.push_back(CountIt); ++CountIt; } struct Compare { bool operator()(map<string, int>::iterator l, map<string, int>::iterator r) { return l->second > r->second; } }; sort(v.begin(),v.end(),Compare()); map<string, int>::iterator it = CountMap.begin(); system("pause"); return 0;} 進擊的Mis_tian
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 井研县| 怀柔区| 辽源市| 荣昌县| 乌兰察布市| 舟山市| 太原市| 扶绥县| 杭锦后旗| 都江堰市| 西和县| 安吉县| 黔东| 石泉县| 望谟县| 宜春市| 栖霞市| 阿拉善左旗| 河曲县| 南和县| 微山县| 库伦旗| 东兰县| 启东市| 万全县| 玉林市| 临漳县| 南溪县| 馆陶县| 景谷| 交口县| 井陉县| 子洲县| 平乐县| 宾阳县| 洞头县| 和硕县| 项城市| 崇州市| 贵德县| 健康|