zoukankan      html  css  js  c++  java
  • 【STL源码剖析读书笔记】【第5章】关联式容器之set、map、multiset和multimap

    一、set

    1、  set的特性是所有元素都会根据元素的键值自动排序,set元素的键值就是实值,实值就是键值。

    2、  不能通过set的迭代器改变set的元素,setiterators是一种constant iterators。

    3、  客户端对set进行元素新增或者删除操作时,操作之前的所有迭代器在操作后都依然有效,被删除的元素的迭代器例外。

    4、  STL set以RB-tree为底层机制,set的操作几乎都是转调用RB-tree的函数而已。

    5、  测试例子

    #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=6
    	cout << "3 count=" << iset.count(3) << endl;//3 count=1
    
    	iset.insert(3);
    	cout << "size=" << iset.size() << endl;//size=6
    	cout << "3 count=" << iset.count(3) << endl;//3 count=1
    
    	iset.insert(7);
    	cout << "size=" << iset.size() << endl;//size=7
    	cout << "3 count=" << iset.count(3) << endl;//3 count=1
    
    	iset.erase(1);
    	cout << "size=" << iset.size() << endl;//size=6
    	cout << "1 count=" << iset.count(1) << endl;//1 count=0
    
    	set<int>::iterator it;
    	for (it = iset.begin(); it != iset.end(); ++it)
    		cout << *it << " "; //2 3 4 5 6 7
    	cout << endl;
    
    	it = find(iset.begin(), iset.end(), 3);
    	if (it != iset.end())
    		cout << "3 found" << endl;//3 found
    
    	it = find(iset.begin(), iset.end(), 1);
    	if (it == iset.end())
    		cout << "1 not found" << endl;//1 not found
    
    	system("pause");
    	return 0;
    }
    

    二、map

    1、  map的特性是所有元素都会根据元素的键值自动排序,map的所有元素都是pair,pai的第一元素是键值,第二元素是实值。

    2、  不能通过map的迭代器改变map的键值,但通过map的迭代器能改变map的实值。因此map的iterators既不是一种const iterators,也不是一种mutable iterators。

    3、  客户端对map进行元素新增或者删除操作时,操作之前的所有迭代器在操作后都依然有效,被删除的元素的迭代器例外。

    4、  STL map以RB-tree为底层机制,map的操作几乎都是转调用RB-tree的函数而已。

    5、  测试例子

    #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 1
    	mp.insert(value);                                   //John 2
    	map<string, int>::iterator it;                      //Kate 4
    	for (it = mp.begin(); it != mp.end(); ++it)         //Lily 3
    		cout << it->first << " " << it->second << endl; //Tom 5
    	                                                   
    	cout << mp["Kate"] << endl;                   //4      
    
    	it = mp.find("John");
    	if (it != mp.end())
    		cout << "John found" << endl;//John found
    	it->second = 8;
    	cout << mp["John"] << endl;//8
    
    	system("pause");
    	return 0;
    }

    三、multiset

    multiset的特性及用法和set完全相同,唯一的差别在于它允许键值重复,因为它的插入操作采用的是RB-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=6
    	cout << "3 count=" << iset.count(3) << endl;//3 count=1
    
    	iset.insert(3); //和set区别的地方
    	cout << "size=" << iset.size() << endl;//size=7
    	cout << "3 count=" << iset.count(3) << endl;//3 count=2
    
    	iset.insert(7);
    	cout << "size=" << iset.size() << endl;//size=8
    	cout << "3 count=" << iset.count(3) << endl;//3 count=2
    
    	iset.erase(1);
    	cout << "size=" << iset.size() << endl;//size=7
    	cout << "1 count=" << iset.count(1) << endl;//1 count=0
    
    	set<int>::iterator it;
    	for (it = iset.begin(); it != iset.end(); ++it)
    		cout << *it << " "; //2 3 3 4 5 6 7
    	cout << endl;
    
    	it = find(iset.begin(), iset.end(), 3);
    	if (it != iset.end())
    		cout << "3 found" << endl;//3 found
    
    	it = find(iset.begin(), iset.end(), 1);
    	if (it == iset.end())
    		cout << "1 not found" << endl;//1 not found
    
    	system("pause");
    	return 0;
    }
    

    四、multimap

    multimap的特性及用法和map完全相同,唯一的差别在于它允许键值重复,因为它的插入操作采用的是RB-tree的inser_equal()。

    #include<map>
    #include<string>
    #include<iostream>
    using namespace std;
    
    int main(){
    	multimap<string, int> mp;//multimap没有下标操作
    	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 1
    	mp.insert(pair<string, int>("John", 8));            //John 2   
    	                                                    //John 8   
    	map<string, int>::iterator it;                      //Kate 4
    	for (it = mp.begin(); it != mp.end(); ++it)         //Lily 3
    		cout << it->first << " " << it->second << endl; //Tom 5
    
    	it = mp.find("John");
    	if (it != mp.end())
    		cout << "John found" << endl; //John found
    	it->second = 8;
    
    	it = mp.find("John");
    	cout << it->second << endl;//8
    
    	system("pause");
    	return 0;
    }

  • 相关阅读:
    linux下TOMCAT自启动
    Tomcat指定(JDK路径)JAVA_HOME而不用环境变量
    Windows防火墙无法正常打开或关闭,上方显示"出于安全原因 某些设置由系统管理员管理”解决方法
    Windows实用操作
    [INS-30131]执行安装程序验证所需的初始设置失败(无法访问临时位置)解决方法!
    Linux 网络一般设置
    集合(set)-Python3
    zip函数-Python 3
    enumerate用法总结-Python 3
    Python3中的字符串函数学习总结
  • 原文地址:https://www.cnblogs.com/ruan875417/p/4558313.html
Copyright © 2011-2022 走看看