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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【STL源码剖析读书笔记】【第5章】关联式容器之set、map、multiset和multimap

發(fā)布時(shí)間:2025/3/21 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【STL源码剖析读书笔记】【第5章】关联式容器之set、map、multiset和multimap 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、set

1、? set的特性是所有元素都會(huì)根據(jù)元素的鍵值自動(dòng)排序,set元素的鍵值就是實(shí)值,實(shí)值就是鍵值。

2、? 不能通過(guò)set的迭代器改變set的元素,setiterators是一種constant iterators。

3、? 客戶端對(duì)set進(jìn)行元素新增或者刪除操作時(shí),操作之前的所有迭代器在操作后都依然有效,被刪除的元素的迭代器例外。

4、? STL set以RB-tree為底層機(jī)制,set的操作幾乎都是轉(zhuǎn)調(diào)用RB-tree的函數(shù)而已。

5、? 測(cè)試?yán)?/p>

#include<set> #include<iostream> using namespace std;int main(){int ia[] = { 5, 3, 4, 1, 6, 2 };set<int> iset(begin(ia), end(ia));cout << "size=" << iset.size() << endl; //size=6cout << "3 count=" << iset.count(3) << endl;//3 count=1iset.insert(3);cout << "size=" << iset.size() << endl;//size=6cout << "3 count=" << iset.count(3) << endl;//3 count=1iset.insert(7);cout << "size=" << iset.size() << endl;//size=7cout << "3 count=" << iset.count(3) << endl;//3 count=1iset.erase(1);cout << "size=" << iset.size() << endl;//size=6cout << "1 count=" << iset.count(1) << endl;//1 count=0set<int>::iterator it;for (it = iset.begin(); it != iset.end(); ++it)cout << *it << " "; //2 3 4 5 6 7cout << endl;it = find(iset.begin(), iset.end(), 3);if (it != iset.end())cout << "3 found" << endl;//3 foundit = find(iset.begin(), iset.end(), 1);if (it == iset.end())cout << "1 not found" << endl;//1 not foundsystem("pause");return 0; }

二、map

1、? map的特性是所有元素都會(huì)根據(jù)元素的鍵值自動(dòng)排序,map的所有元素都是pair,pai的第一元素是鍵值,第二元素是實(shí)值。

2、? 不能通過(guò)map的迭代器改變map的鍵值,但通過(guò)map的迭代器能改變map的實(shí)值。因此map的iterators既不是一種const iterators,也不是一種mutable iterators。

3、? 客戶端對(duì)map進(jìn)行元素新增或者刪除操作時(shí),操作之前的所有迭代器在操作后都依然有效,被刪除的元素的迭代器例外。

4、? STL map以RB-tree為底層機(jī)制,map的操作幾乎都是轉(zhuǎn)調(diào)用RB-tree的函數(shù)而已。

5、? 測(cè)試?yán)?/p> #include<map> #include<string> #include<iostream> using namespace std;int main(){map<string, int> mp;mp["Jack"] = 1;mp["John"] = 2;mp["Lily"] = 3;mp["Kate"] = 4;//按鍵值字典序排序pair<string, int> value("Tom", 5); //Jack 1mp.insert(value); //John 2map<string, int>::iterator it; //Kate 4for (it = mp.begin(); it != mp.end(); ++it) //Lily 3cout << it->first << " " << it->second << endl; //Tom 5cout << mp["Kate"] << endl; //4 it = mp.find("John");if (it != mp.end())cout << "John found" << endl;//John foundit->second = 8;cout << mp["John"] << endl;//8system("pause");return 0; }

三、multiset

multiset的特性及用法和set完全相同,唯一的差別在于它允許鍵值重復(fù),因?yàn)樗牟迦氩僮鞑捎玫氖荝B-tree的inser_equal()。

#include<set> #include<iostream> using namespace std;int main(){int ia[] = { 5, 3, 4, 1, 6, 2 };multiset<int> iset(begin(ia), end(ia));cout << "size=" << iset.size() << endl; //size=6cout << "3 count=" << iset.count(3) << endl;//3 count=1iset.insert(3); //和set區(qū)別的地方cout << "size=" << iset.size() << endl;//size=7cout << "3 count=" << iset.count(3) << endl;//3 count=2iset.insert(7);cout << "size=" << iset.size() << endl;//size=8cout << "3 count=" << iset.count(3) << endl;//3 count=2iset.erase(1);cout << "size=" << iset.size() << endl;//size=7cout << "1 count=" << iset.count(1) << endl;//1 count=0set<int>::iterator it;for (it = iset.begin(); it != iset.end(); ++it)cout << *it << " "; //2 3 3 4 5 6 7cout << endl;it = find(iset.begin(), iset.end(), 3);if (it != iset.end())cout << "3 found" << endl;//3 foundit = find(iset.begin(), iset.end(), 1);if (it == iset.end())cout << "1 not found" << endl;//1 not foundsystem("pause");return 0; }

四、multimap

multimap的特性及用法和map完全相同,唯一的差別在于它允許鍵值重復(fù),因?yàn)樗牟迦氩僮鞑捎玫氖荝B-tree的inser_equal()。

#include<map> #include<string> #include<iostream> using namespace std;int main(){multimap<string, int> mp;//multimap沒(méi)有下標(biāo)操作mp.insert(pair<string, int>("Jack", 1));mp.insert(pair<string, int>("John", 2));mp.insert(pair<string, int>("Lily", 3));mp.insert(pair<string, int>("Kate", 4));//按鍵值字典序排序mp.insert(pair<string, int>("Tom", 5)); //Jack 1mp.insert(pair<string, int>("John", 8)); //John 2 //John 8 map<string, int>::iterator it; //Kate 4for (it = mp.begin(); it != mp.end(); ++it) //Lily 3cout << it->first << " " << it->second << endl; //Tom 5it = mp.find("John");if (it != mp.end())cout << "John found" << endl; //John foundit->second = 8;it = mp.find("John");cout << it->second << endl;//8system("pause");return 0; }

轉(zhuǎn)載于:https://www.cnblogs.com/ruan875417/p/4558313.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的【STL源码剖析读书笔记】【第5章】关联式容器之set、map、multiset和multimap的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。