关联容器
要学习关联容器,就必须先知道什么是pair,pair是关联容器的某一对键值对的表示,也就是关联容器的value_type对象。
关联容器通过支持键值对的存储,从而可以高效地查找和读取元素,基本的关联容器有map和set。
关联容器拥有顺序容器的大部分接口,没有的接口都是和顺序有关的接口(因关联容器内的元素在容器内无序),关联容器没有front、back、push_back、push_front、pop_back和pop_front操作。
map类定义的类型:
map<K,V>::key_type 用作索引的键类型
map<K,V>::mapped_type 用作值的类型
map<K,V>::value_type pair类型,表示容器的一个键值对
map容器支持下标索引操作,下标为键值,通过下标操作可以访问元素,修改元素值,增加元素(vector不行)。通过下标的操作返回值是mapped_type类型,是一个引用类型值,所以通过它可以修改键值对中的元素值。
查看某元素是否在容器内可以使用find或者count来实现,注意的是find返回值是容器的迭代器,而count返回值是intint型变量。
set关联容器内只存储键值,set翻译过来是集合,它和数学中定义的集合差不多是一样的,无序、元素值不能重复。它和vector的不同点其中一处就是vector允许元素值相等,而set则不允许,可以初步理解set为map<T,const bool>类型。
练习代码:
// CorrelationContainer.cpp : 定义控制台应用程序的入口点。 //关联容器 #include "stdafx.h" #include <iostream> #include <utility> #include <string> #include <map> #include <set> using namespace std; //pair的使用示例 void TestPair() { //创建pair对象 pair<string, string> tel("IT05", "15527******"); //first和second成员,比较两个pair对象是就是依次比较这两个值 cout<< "姓名:" << tel.first << " 电话:" << tel.second << endl; //利用make_pair将两个值转换为一个pair pair<int, string> dom = make_pair(412, "IT05"); cout<< "宿舍号:" << dom.first << " 姓名:" << dom.second << endl; } //关联容器map的使用 void TestMap() { map<string,string> tel_book; //添加元素,insert tel_book.insert(tel_book.begin(),make_pair("IT05","15527******")); tel_book.insert(make_pair("IT06","15528******")); tel_book.insert(make_pair("IT07","15529******")); tel_book.insert(tel_book.begin(),make_pair("IT08","15520******")); tel_book.insert(make_pair("IT09","15521******")); //遍历map for( map<string,string>::const_iterator i=tel_book.begin(); i!=tel_book.end(); i++ ) { //此时的i为value_type类型,是pair类型的指针 cout << "姓名:" << i->first << " 电话:" << i->second ; //示例key_type和mapped_type的使用 map<string,string>::key_type key = i->first; map<string,string>::mapped_type value = i->second; cout << " 姓名:" << key << " 电话:" << value << endl ; } //赋值,修改元素的值 tel_book["IT05"] = "1342656******"; //使用键值下标访问容器内容 cout << "修改后IT05的电话号码是:" << tel_book["IT05"] << endl; //使用下标的访问可以添加元素 tel_book["ITMe"] = "1595672******"; tel_book.erase("IT06"); cout << "下标添加ITMe后容器的内容为:" << endl; for( map<string,string>::const_iterator i=tel_book.begin(); i!=tel_book.end(); i++ ) { //此时的i为value_type类型,是pair类型的指针 cout << "姓名:" << i->first << " 电话:" << i->second << endl; } //删除元素 tel_book.erase("IT06"); cout << "删除IT06后容器的内容为:" << endl; for( map<string,string>::const_iterator i=tel_book.begin(); i!=tel_book.end(); i++ ) { //此时的i为value_type类型,是pair类型的指针 cout << "姓名:" << i->first << " 电话:" << i->second << endl; } //insert返回值,只有insert的形参为pai才有下面的返回值 //pair<map<K,V>::iterator, bool> cout << "insert测试:" << endl; pair<map<string,string>::iterator, bool> in1 = tel_book.insert(make_pair("IT05","1234")); cout << "IT05修改没?:" << in1.second << endl;//输出0,表示没有插入 pair<map<string,string>::iterator, bool> in2 = tel_book.insert(make_pair("IT00","1233456****")); cout << "IT00修改没?:" << in2.second << endl;//输出1,表示插入了 if( in2.second ) { cout << "新插入的值为:<" << in2.first->first << "," << in2.first->second << ">" << endl; } //结果是IT05的号码没变,IT00添加到电话薄 for( map<string,string>::const_iterator i=tel_book.begin(); i!=tel_book.end(); i++ ) { //此时的i为value_type类型,是pair类型的指针 cout << "姓名:" << i->first << " 电话:" << i->second << endl; } //查看某元素是否在容器内,不能使用下标操作,因为这会导致把新元素插入到容器内 //find使用示例,count可以返回出现的次数,所以也可以用count实现find if( tel_book.find("IT05") != tel_book.end() ) { cout << "IT05在容器内!" << endl; } else cout << "IT05不在容器内!" << endl; if( tel_book.find("IT0") != tel_book.end() ) { cout << "IT0在容器内!" << endl; } else cout << "IT0不在容器内!" << endl; if( tel_book.count("IT05") ) { cout << "IT05在容器内!" << endl; } else cout << "IT05不在容器内!" << endl; } void TestSet() { //set相当于map<T,bool>类型 cout << "set测试:" << endl; set<int> s; //不能通过下标来访问 for( int i=0; i<10; i++ ) s.insert(i); for( set<int>::const_iterator i=s.begin(); i!=s.end(); i++ ) cout << *i << " "; cout << endl; /*********************************************************** * *s.begin() = 11; //错误,不能修改set内的元素值 */ } int main(int argc, _TCHAR* argv[]) { TestPair(); TestMap(); TestSet(); getchar(); return 0; }
结果图: