zoukankan      html  css  js  c++  java
  • 【c++ primer读书笔记】【第11章】关联容器

    1、关联容器类型

    按关键字有序保存元素

    map

    关联数组:保存关键字-值对应

    set

    关键字既值,既只保存关键字的容器

    multimap

    关键字可重复出现的map

    multiset

    关键字可重复出现的set

    无序集合

    unordered_map 

    hash函数组织的map 

    unordered_set

    hash函数组织的set

    unordered_multimap

    hash组织的map:关键字可重复出现

    unordered_multiset

    hash组织的set:关键字可重复出现

    运用map和set的例子,统计单词程序:

    #include<iostream>
    #include<map>
    #include<set>
    #include<sstream>
    #include<string>
    using namespace std;
    
    int main(){
    	map<string, size_t> word_count;
    	set<string> exclude{ "an", "the" };
    	string str = "although before an and aaa although the aaa";
    	stringstream ss(str);
    	string word;
    	while (ss >> word){
    		if (exclude.find(word) == exclude.end()) //只统计不在exclude中的单词
    			++word_count[word];
    	}
    	for (const auto& w : word_count)
    		cout << w.first << " occurs " << w.second << ((w.second>1) ? " times" : " time") << endl;
    
    	system("pause");
    	return 0;
    }
    

    2、定义关联容器

    定义一个map时,必须指明关键字类型和值类型,定义一个set时,只需指明关键字类型。

    初始化关联容器:

    map<string,size_t> wor(word_count); //空容器
    map<string,string> authors={{"joyce","jamese"},{"Austen","jane"}};//列表初始化,VS2013支持
    map<string,string> au(authors);//用容器authors初始化au
    

    3对于有序容器map,multimap,setmultiset,关键字类型必须定义元素的比较方法。默认情况下,标准库使用关键字类型<运算符来比较两个关键字。

    4、pair类型

    一个pair保存两个数据成员,一个是first,一个是secondpair的数据成员是public的。

    pair的默认构造函数对数据成员进行值初始化。

    5、关联容器额外的类型别名

    key_type  

    此容器类型的关键字类型

    mapped_type 

    每个关键字关联的类型,只适用于map

    value_type

    对于set,key_type相同

    对于map,pair<const key_type,mapped_type>

    6set的迭代器是const的。虽然set类型同时定义了iteratorconst_iterator,但两种类型都是只允许只读访问set中的元素。

    set<int> iset={1,2,3,4,5};
    set<int>::iterator it=iset.begin();
    if(it!=iset.end()){
    	*it=42;   //错误:set中的关键字是只读的
    	cout<<*it<<endl;  //正确
    }
    

    7、添加元素、删除元素

    #include<iostream>
    #include<map>
    #include<string>
    #include<utility>
    using namespace std;
    
    int main(){
    	map<string, size_t> word_count;
    	map<string, size_t> wc = { { "ff", 2 }, { "tt", 1 } }; //在VS2013中支持
    	word_count.insert({ "aa", 1 });  //c.insert(v),在VS2013中支持
    	word_count.insert(make_pair("cc", 1));  //c.insert(v)
    	word_count.insert(pair<string, size_t>("bb", 1));  //c.insert(v)
    	word_count.insert(map<string, size_t>::value_type("aa", 1));  //c.insert(v)
    
    	word_count.insert(wc.begin(), wc.end());//c.insert(b,e),b和e是迭代器,表示一个c::value_type类型值的范围,返回void
    	word_count.emplace("qq", 5);//c.emplace(args),对于map和set,只有当元素关键字不在c中才插入。
    	                           //返回一个pair,包含一个迭代器,指向关键字的元素,以及一个指示插入是否成功的bool值  
    
    	for (const auto& w : word_count)
    		cout << w.first << " " << w.second << endl;
    	cout << "------------------------------" << endl;
    	word_count.erase("bb");  //c.erase(k),从c中删除每个关键字为k的元素,返回一个size_type类型值,表示删除元素的数量
    	word_count.erase(word_count.begin());//c.erase(p),从c中删除迭代器p指定的元素,返回p之后元素的迭代器
    	for (const auto& w : word_count)
    		cout << w.first << " " << w.second << endl;
    	cout << "------------------------------" << endl;
    	word_count.erase(++word_count.begin(), --word_count.end());//c.erase(b,e),删除迭代器对b和e所表示范围中的元素,返回e
        for (const auto& w : word_count)
    		cout << w.first << " " << w.second << endl;
    
    	system("pause");
    	return 0;
    }
    

    运行结果:

    8、map的下标操作

    mapunordered_map容器提供了下标运算符和一个对应的at函数。map和其他下标运算符不同的是,如果关键字不在容器中,它会创建一个元素并插入到map中去,关联值将进行初始化。

    map<string,size_t> word_count;
    word_count["aa"]=1; //插入关键字为"aa"的元素,关联值进行值初始化,然后将1赋予它
    cout<<word_count["aa"]<<endl; //将"aa"作为下标提取元素,输出1
    ++word_count["aa"]; //标提取元素,将其加1
    cout<<word_count["aa"]<<endl;//将"aa"作为下标提取元素,输出2
    

    9、访问元素

    #include<iostream>
    #include<set>
    #include<string>
    #include<utility>
    using namespace std;
    
    int main(){
    	int ia[]={1,2,3,4,5,6,7,8,9};
    	set<int> iset(begin(ia),end(ia));
    	
    	iset.find(1); //返回一个迭代器,指向key==1的元素
    	iset.find(11); //返回一个迭代器,其值等于iset.end()
    	iset.count(1); //c.count(k),返回关键字等于k的元素数量,返回1
    	iset.count(11); //c.count(k),返回关键字等于k的元素数量,返回0
    	
    	auto low=iset.lower_bound(2);//c.lower_bound(k),返回一个迭代器,指向第一个关键字不小于k的元素
    	auto upper=iset.upper_bound(5);//c.lower_bound(k),返回一个迭代器,指向第一个关键字大于k的元素
    	while(low!=upper)
    		cout<<*low++<<" ";
    
    	auto range=iset.equal_range(3); //c.equal_range(k),返回一个迭代器pair,表示关键字等于k的元素的范围
    	cout<<endl;
    	system("pause");                       
    	return 0;                              
    }
    

    lower_bound和upper_bound不适用于无序容器。
    10、c++11定义了4个无序关联容器,这些容器不是使用比较运算符来组织元素的,而是使用一个哈希函数和关键字类型的==运算符。

    使用无序容器 unordered_map统计单词

    #include<iostream>
    #include<unordered_map>
    #include<set>
    #include<sstream>
    #include<string>
    using namespace std;
    
    int main(){
    	unordered_map<string, size_t> word_count; //统计单词出现次数,但单词不会按字典序排列
    	set<string> exclude{ "an", "the" };
    	string str = "although before an and aaa although the aaa";
    	stringstream ss(str);
    	string word;
    	while (ss >> word){
    		if (exclude.find(word) == exclude.end()) //只统计不在exclude中的单词
    			++word_count[word];
    	}
    	for (const auto& w : word_count)
    		cout << w.first << " occurs " << w.second << ((w.second>1) ? " times" : " time") << endl;
    
    	system("pause");
    	return 0;
    }
    
    11、管理桶

    无序容器在组织形式上为一组桶,每个桶保存零个或多个元素。使用一个哈希函数将元素映射到桶。所有相同关键字的元素会在同一个桶中。无序容器的质量依赖于哈希函数的质量和桶的大小和数量。

    #include<iostream>
    #include<unordered_set>
    using namespace std;
    
    int main(){
    	unordered_multiset<int> iset{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7 };
    
    	cout << iset.bucket_count() << endl;   //正在使用的桶的数目
    	cout << iset.max_bucket_count() << endl;//容纳的最多的桶的数目
    	cout << iset.bucket_size(1) << endl; //第1个桶中有多少个元素
    	cout << iset.bucket(4) << endl;  //关键字为4的元素在哪个桶中
    
    	cout << iset.load_factor() << endl; //每个桶的平均元素数量,返回float值
    	cout << iset.max_load_factor() << endl;//维护平均桶大小,返回float值。需要时添加新的桶使load_factor <= max_load_factor  
    	iset.rehash(20); //重组存储,使得bucket_count>=20且bucket_count>size/max_load_factor()
    	iset.reserve(20);//重组存储,使得iset可以保存20个元素且不必rehash
    
    	system("pause");
    	return 0;
    }
    

    12、无需容器对关键字类型的要求

    无序容器使用关键字==来比较元素,它们还使用了一个hash<key_type>类型的对象来生成每个元素的hash值。

    我们不能定义关键字类型为自定义类类型的无序容器。与容器不同,不能直接使用hash模板,而是必须提供我们自己的hash模板。

    为了将自己的类当作关键字,我们要自己提供==运算符和hash值计算函数。

  • 相关阅读:
    openstack newton 版本 horizon二次开发
    ubuntu 远程root登录
    记录一次用户态踩内存问题
    (leetcode)二叉树的前序遍历-c语言实现
    通过blacklist来禁用驱动
    最小栈问题
    判断是否为环形链表
    按照层次序列创建二叉树,并判断二叉树是否为二叉搜索树
    操作系统交付时需要做哪些安全检查项
    RDMA相关的技术网站
  • 原文地址:https://www.cnblogs.com/ruan875417/p/4495571.html
Copyright © 2011-2022 走看看