问题
单词计数程序,有序和无序版本.
有序源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /************************************************************************* > File Name: test.11.39.cpp > Author: puruidong > Mail: 1@w1520.com > Created Time: 2014年07月15日 ************************************************************************/ #include<iostream> #include<map> using namespace std; /********************************* 单词计数程序,字典序. *****************************************/ int main() { cout << "单词计数程序,字典序:" << endl; map<string,int> map; string pa; while(cin >> pa) { ++map[pa]; } cout << "输出计算结果:" << endl; for(const auto &s : map) { cout << s.first << " ------ " << s.second << endl; } return 0; } |
无序源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /************************************************************************* > File Name: test.11.38.cpp > Author: puruidong > Mail: 1@w1520.com > Created Time: 2014年07月15日 ************************************************************************/ #include<iostream> #include<unordered_map> #include<iterator> using namespace std; /***************************************** 用unordered_map重写单词计数程序 **************************************************/ int main() { unordered_map<string,int> map; string pa; while(cin >> pa) { ++map[pa];//自增. } cout << "***********************\n统计结果:\n******************************" << endl; for(const auto &s : map) { cout << s.first << "\t************************\t" << s.second << endl; } cout << "输出桶的数目:" << map.bucket_count() << endl; cout << "最大能容纳的最多的桶的数量:" << map.max_bucket_count() << endl; cout << "第1个桶中有多少个元素:" << map.bucket_size(1) << endl; cout << "关键字a在哪个桶中:" << map.bucket("a") << endl; cout << "每个桶的平均元素数量:" << map.load_factor() << endl; cout << "容器试图维护的平均桶大小:" << map.max_load_factor() << endl; return 0; } |