STL标准库的关联容器

关联容器支持高效的关键字查找和访问。map中的元素是一些关键字-值(key-value)对:关键字起到索引的作用,值则是表示与索引相关联的数据。set中每个元素只包含一个关键字,可以说set是一个特殊的map。

标准库提供8个关联容器,允许重复关键字的容器的名字中都包含单词multi;不保持关键字按顺序存储的容器的名字都以unordered开头。因此一个unordered_multi_set是一个允许重复关键字,元素无序保存的集合。

类型map和multimap定义在头文件map中;set和multiset定义在头文件set中;无序容器则定义在头文件unordered_map和unordered_set中。

先来一个set使用的简单例子:

// set_ex.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <set>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	//set自动排序
	set<int> myset;
	multiset<int> mset;
	for(int i=0; i<10; i++)
		mset.insert(10-i);
	mset.insert(7);
	for(multiset<int>::iterator it=mset.begin(); it!=mset.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
	for(int i=0; i<10; i++)
	{
		myset.insert(10-i);
	}
	myset.insert(5);
	set<int>::iterator itor,upitor,downitor;
	for(itor=myset.begin(); itor!=myset.end(); itor++)
	{
		cout<<*itor<<" ";
	}
	cout<<endl;
	//查询、删除数据
	int num=5;
	cout<<"find and delete element "<<num<<endl;
	itor=myset.find(num);
	if(itor!=myset.end())
	{
		myset.erase(itor);
	}
	for(itor=myset.begin(); itor!=myset.end(); itor++)
	{
		cout<<*itor<<" ";
	}
	cout<<endl;
	//元素边界
	num=5;
	upitor=myset.lower_bound(num);
	downitor=myset.upper_bound(num);
	cout<<num<<" lower bound is "<<*upitor<<endl;
	cout<<num<<" upper bound is "<<*downitor<<endl;
	pair<set<int>::iterator, set<int>::iterator> ret=myset.equal_range(7);
	cout<<num<<" lower bound is "<<*ret.first<<endl;
	cout<<num<<" upper bound is "<<*ret.second<<endl;
	//check数据是否在容器中
	if(myset.count(num)>0)
	{
		cout<<num<<" is an element of myset."<<endl;
	}
	else
	{
		cout<<num<<" is not an element of myset."<<endl;
	}
	//输出容器大小
	cout<<"set size: "<<myset.size()<<endl;
	for(itor=myset.begin(); itor!=myset.end(); itor++)
	{
		cout<<*itor<<" ";
	}
	cout<<endl;
	system("pause");
	return 0;
}

一个map使用的简单例子:

// map_ex.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <map>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	map<char, int> mymap;
	map<char, int>::iterator itor;
	pair<map<char, int>::iterator, bool> ret1;
	pair<map<char, int>::iterator,map<char, int>::iterator> ret;
	//插入元素
	mymap['a']=10;
	mymap['b']=20;
	mymap['c']=30;
	mymap['d']=40;
	ret1=mymap.insert(pair<char, int>('c',500));
	if(ret1.second==false)
	{
		cout<<"element 'c' already existed";
		cout<<" with a value of "<<ret1.first->second<<endl;
	}
	//寻找、删除
	itor=mymap.find('d');
	mymap.erase(itor);
	//边界
	ret=mymap.equal_range('b');
	cout<<"lower bound points to: ";
	cout<<ret.first->first<<" => "<<ret.first->second<<endl;
	cout<<"upper bound points to: ";
	cout<<ret.second->first<<" => "<<ret.second->second<<endl;
	//输出容器元素
	cout<<"mymap contains: ";
	for(itor=mymap.begin(); itor!=mymap.end(); itor++)
	{
		cout<<(*itor).first<<" => "<<(*itor).second<<endl;
	}
	system("pause");
	return 0;
}
再来一个multimap查找元素的例子

// query.cpp : 定义控制台应用程序的入口点。
//在multimap中查找元素

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	multimap<string,string> mymap;
	string path="list.txt";
	string printpath="print.txt";
	string text;
	ifstream file;
	file.open(path);
	//读取txt文件
	while(file)
	{
		if(getline(file,text))
		{
			int n=text.find_first_of(" ");
			string author=text.substr(0,n);
			string book=text.substr(n);
			//向multimap添加元素
			mymap.insert(make_pair(author,book));
		}
	}
	//利用迭代器打印multimap中的数据
	cout<<"打印按字母顺序排好的文本,按“作者 著作”排序:"<<endl;
	multimap<string,string>::iterator itor;
	for(itor=mymap.begin();itor!=mymap.end(); itor++)
	{
		cout<<(*itor).first<<"  "<<(*itor).second<<endl;
	}
	cout<<"********************我是万恶的分割线********************"<<endl;
	string search_item("jiang");
	cout<<"打印查找到"<<search_item<<"作者的著作"<<endl;
	auto entries=mymap.count(search_item);
	auto copyentries=entries;
	auto iter=mymap.find(search_item);
	auto copyiter=iter;
	while(entries)
	{
		cout<<iter->first<<"  "<<iter->second<<endl;
		++iter;
		--entries;
	}
	//复习下文本操作,把上述东西打印到文本
	ofstream File;
	File.open(printpath,ios_base::trunc);
	while(copyentries)
	{
		File<<copyiter->first<<"  "<<copyiter->second<<endl;
		++copyiter;
		--copyentries;
	}
	system("pause");
	return 0;
}
结果:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值