日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(chap 11)关联容器

發布時間:2025/3/21 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 的關鍵字類型必須定義 比較元素的方法(默認使用 “<” 來比較)
class Sales_data { public:Sales_data() {}bool operator < (const Sales_data& lhs){return true;}bool static compareIsbn(const Sales_data& lhs, const Sales_data& rhs){return true;} }; multiset<Sales_data, decltype(Sales_data::compareIsbn)*> bookstore(Sales_data::compareIsbn);

4.pair類型

  • 鍵值對
  • pair 的默認構造函數會 first 和 second 數據成員進行值初始化
  • pair的數據成員是public
std::pair<string, string> author{ " huang", "cheng" };

定義(構造函數):

定義說明
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)关联容器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。