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;
}
結果:






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章