C++ Primer 5th笔记(chap 11)关联容器
生活随笔
收集整理的這篇文章主要介紹了
C++ Primer 5th笔记(chap 11)关联容器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?map
?multimap
?set
?multiset
?set
?unordered_map
?unordered_set
?unordered_multimap
?unordered_multiset
| map | 有序性,其元素的有序性在很多應用中都會簡化很多的操作,內部實現一個紅黑書使得map的很多操作在lgnlgn的時間復雜度下就可以實現,因此效率非常的高。 | 空間占用率高,因為map內部實現了紅黑樹,雖然提高了運行效率,但是因為每一個節點都需要額外保存父節點,孩子節點以及紅/黑性質,使得每一個節點都占用大量的空間 對于那些有順序要求的問題,用map會更高效一些 |
| unordered_map | 內部實現了哈希表,因此其查找速度非常的快 | 哈希表的建立比較耗費時間 對于查找問題,unordered_map會更加高效一些,因此遇到查找問題,常會考慮一下用unordered_map |
關聯容器有一些順序容器不支持的操作
關聯容器有類型別名
map<K, V>::key_type //在map容器中,用作索引的鍵的類型 set<K>::key_type map<K, V>::mapped_type //在map容器中,鍵所關聯的值的類型 map<K, V>::value_type //一個pair類型,它的first元素具有key_type類型,second元素具有mapped_type類型1. map
map<string, size_t> word_count; // empty map from string to size_t string word; while (cin >> word)++word_count[word];for (const auto& w : word_count)cout << w.first << " occurs " << w.second << " times" << endl;// get an iterator positioned on the first element auto map_it = word_count.cbegin(); // compare the current iterator to the off-the-end iterator while (map_it != word_count.cend()) {// dereference the iterator to print the element key--value pairscout << map_it->first << " occurs "<< map_it->second << " times" << endl;++map_it; // increment the iterator to denote the next element }2. set
map<string, size_t> word_count; // empty map from string to size_t set<string> exclude = {"The", "But"};string word; while (cin >> word)if(exclude.find(word) == exclude.end())++word_count[word];for (const auto& w : word_count)cout << w.first << " occurs " << w.second << " times" << endl;// get an iterator positioned on the first element auto map_it = word_count.cbegin(); // compare the current iterator to the off-the-end iterator while (map_it != word_count.cend()) {// dereference the iterator to print the element key--value pairscout << map_it->first << " occurs "<< map_it->second << " times" << endl;++map_it; // increment the iterator to denote the next element }- set 中的關鍵字也是const的。所以說用一個set 的迭代器來讀取元素的值,但不能修改其值。
3.multiset mulimap
std::map<string, string> authors = { {"Joyce", "James"} }; std::map<string, string> word_count = authors; std::multimap<string, string> word_count2(authors.cbegin(), authors.cend());std::set<string> exclude = { "the", "but" }; std::set<string> exclude1(exclude.cbegin(), exclude.cend());std::multiset<string> exclude2(exclude.cbegin(), exclude.cend());- map, multimap, set, and multiset 的關鍵字類型必須定義 比較元素的方法(默認使用 “<” 來比較)
4.pair類型
- 鍵值對
- pair 的默認構造函數會 first 和 second 數據成員進行值初始化
- pair的數據成員是public
定義(構造函數):
| pair<T1, T2> p1; | 創建一個空的pair對象(使用默認構造),它的兩個元素分別是T1和T2類型,采用值初始化 |
| pair<T1, T2> p1(v1, v2); | 創建一個pair對象,它的兩個元素分別是T1和T2類型,其中first成員初始化為v1,second成員初始化為v2 |
| make_pair(v1, v2); | 以v1和v2的值創建一個新的pair對象,其元素類型分別是v1和v2的類型 |
| p1 < p2 | 創建一個pair對象,它的兩個元素分別是T1和T2類型,其中first成員初始化為v1,second成員初始化為v2 |
| p1 == p2 | 兩個pair對象間的小于運算,其定義遵循字典次序:如 p1.first < p2.first 或者 !(p2.first < p1.first) && (p1.second < p2.second) 則返回true |
| p1.first | 返回對象p1中名為first的公有數據成員 |
| p1.second | 返回對象p1中名為second的公有數據成員 |
總結
以上是生活随笔為你收集整理的C++ Primer 5th笔记(chap 11)关联容器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Primer 5th笔记(cha
- 下一篇: C++ Primer 5th笔记(cha