C++ map

本文详细介绍了C++标准库中的map容器,包括构造、赋值、迭代器操作、插入、删除、查找、大小、交换等核心函数的用法,并对比了与multimap的区别。通过实例展示了map在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

map

标准的STL关联式容器分为set(集合)和map(映射表)两大类。以及这两大类的衍生体multiset(多建集合)和multimap(多键映射表)。这些容器的底层机制均是以RB-tree(红黑树)完成的。RB-tree也是一个独立容器,但并不对外开放使用。
map的特性是,所有元素都会根据元素的键值自动被排序。map的所有元素都是pair,同时拥有实值(value)和键值(key)。pair的第一元素被视为键值。第二元素被视为实值。map不允许两个元素拥有相同的键值。
我们可以通过map的迭代器改变map的元素内容吗?如果想要修正元素的键值,答案是不行,因为map元素的键值关系到map元素的排序规则。任意改变map元素键值,将会严重破坏map组织。但是如果想要修正元素的实值,答案是可以的,因为map元素的实值并不影响map的元素的排列规则。因此,map iterators 既不是一种constant iterator,也不是一种mutable iterators

map拥有和list相同的某些性质,当客户端对它进行元素新增操作(insert)或删除操作(erase)时,操作之前的所有迭代器,在操作完成之后都依然有效。当然,被删除的那个元素的迭代器必然是个例外。
由于RB-tree是一种平衡二叉搜索树,自动排序的效果很是不错,所以标准的STL map 即以RB-tree为底层机制。又由于map所开放的各种操作接口,RB-tree也都提供了,所以几乎所有的map的操作行为,都只是转调用RB-tree的操作行为而已。
multimap 的特性以及用法与map完全相同,唯一的差别在于它允许键值重复,因此它的插入操作采用的是底层机制RB-tree的insert_equal()而非insert_unique()。

map::map()函数 功能:构造map
map::operator=()函数 功能:赋值map
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::operator=()函数 赋值map 
	map<char,int>first;
	map<char,int>second;
	first['x']=8;
	first['y']=18;
	first['z']=28;
	second=first;//second now contains 3 ints
	first=map<char,int>();//and first is now empty
	cout<<"size of first: "<<int(first.size())<<endl;
	cout<<"size of second: "<<int(second.size())<<endl;
}

运行结果

size of first: 0
size of second: 3

map::begin()函数 功能:返回指向map头部的迭代器
map::end()函数 功能:返回指向map末尾的迭代器
map::rbegin()函数 功能:返回一个指向map尾部的逆向迭代器
map::rend()函数 功能:返回一个指向map头部的逆向迭代
#include<iostream>
#include<map>
using namespace std;
 
int main(){
	//map::begin()函数 功能:返回指向map头部的迭代器
	//map::end()函数 功能:返回指向map末尾的迭代器
	//map::rbegin()函数 功能:返回一个指向map尾部的逆向迭代器
	//map::rend()函数 功能:返回一个指向map头部的逆向迭代器
	map<char,int>mymap;
	map<char,int>::iterator it;
	mymap['a']=100;
	mymap['b']=200;
	mymap['c']=300;
	//show content;
	for(it=mymap.begin();it!=mymap.end();it++){
		cout<<(*it).first<<":"<<(*it).second<<endl;
	}
	map<char,int>::reverse_iterator rit;
	for(rit=mymap.rbegin();rit!=mymap.rend();rit++){
		cout<<(*rit).first<<": "<<(*rit).second<<endl;
		
	} 
}

运行结果

a:100
b:200
c:300
c: 300
b: 200
a: 100

map::insert()函数 功能:插入元素
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::insert()函数 功能:插入元素
	map<char,int>mymap;
	map<char,int>::iterator it;
	pair<map<char,int>::iterator,bool>ret;
	//first insert function vertion(single parameter):
    mymap.insert(pair<char,int>('a',10));
	mymap.insert(pair<char,int>('z',20));
	ret=mymap.insert(pair<char,int>('z',60));
	if(ret.second==false){
		cout<<"element  'z' already existed"<<endl;
		cout<<"with a value of "<<ret.first->second<<endl;
	}
	//second insert function version(with hint position):
	it=mymap.begin();
	mymap.insert(it,pair<char,int>('b',11));//max efficiency inserting
	mymap.insert(it,pair<char,int>('c',12));//no max efficiency inserting
	//third insert function version(range insertion)
	map<char,int>anothermap;
	anothermap.insert(mymap.begin(),mymap.find('c'));
	//show contents:
	cout<<"mymap contains:"<<endl;
	for(it=mymap.begin();it!=mymap.end();it++){
		cout<<(*it).first<<":"<<(*it).second<<endl;
	}
	cout<<"anothermap contains:"<<endl;
	for(it=anothermap.begin();it!=anothermap.end();it++){
		cout<<(*it).first<<":"<<(*it).second<<endl;
	}
}

运行结果

element  'z' already existed
with a value of 20
mymap contains:
a:10
b:11
c:12
z:20
anothermap contains:
a:10
b:11

map::erase()函数 功能:删除一个元素
#include<iostream>
#include<map>
using namespace std;
 
int main(){
	//map::erase()函数 功能:删除一个元素
	map<char,int>mymap;
	map<char,int>::iterator it;
	// insert some values
	mymap['a']=1;
	mymap['b']=2;
	mymap['c']=3;
	mymap['d']=4;
	mymap['e']=5;
	mymap['f']=6;
	mymap['g']=7;
	it=mymap.find('b');
	mymap.erase(it);//erasing by iterator
	mymap.erase('c');//erasing by key
	it=mymap.find('e');
	mymap.erase(it,mymap.end());//erasing by range
	//show content
	for(it=mymap.begin();it!=mymap.end();it++){
		cout<<(*it).first<<":"<<(*it).second<<endl;
	}
}

运行结果

a:1
d:4

map::find()函数 功能:查找一个元素,如果找到返回改元素的迭代器
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::find()函数 功能:查找一个元素,如果找到返回改元素的迭代器
    map<char,int>mymap;
	map<char,int>::iterator it;
	// insert some values
	mymap['a']=1;
	mymap['b']=2;
	mymap['c']=3;
	mymap['d']=4;
	mymap['e']=5;
	mymap['f']=6;
	mymap['g']=7;
	it=mymap.find('b');
	mymap.erase(it);
	mymap.erase(mymap.find('d'));
	while(!mymap.empty()){
//		cout<<(*mymap.begin()).first<<":"<<(*mymap.begin()).second<<endl;
		cout<<mymap.begin()->first<<":"<<mymap.begin()->second<<endl;
		mymap.erase(mymap.begin());
	}
}

运行结果

a:1
c:3
e:5
f:6
g:7

map::clear() 函数 功能:删除所有元素
#include<iostream>
#include<map>
using namespace std;
int main(){
    //map::clear() 函数 功能:删除所有元素
	map<char,int>mymap;
	map<char,int>::iterator it;
	mymap['m']=1998;
	mymap['z']=11;
	mymap['h']=3;
	cout<<"mymap contains:\n";
	for(it=mymap.begin();it!=mymap.end();it++){
		cout<<(*it).first<<"->"<<(*it).second<<endl;
	}
	mymap.clear();
	mymap['a']=1999;
	mymap['b']=9;
	mymap['c']=15;
	cout<<"mymap contains:\n";
	for(it=mymap.begin();it!=mymap.end();it++){
		cout<<(*it).first<<"->"<<(*it).second<<endl;
	}
	 
}

运行结果

mymap contains:
h->3
m->1998
z->11
mymap contains:
a->1999
b->9
c->15

map::empty()函数 功能:如果map为空则返回true
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::empty()函数 功能:如果map为空则返回true
	map<char,int>mymap;
	map<char,int>::iterator it;
	// insert some values
	mymap['a']=1;
	mymap['b']=2;
	mymap['c']=3;
	mymap['d']=4;
	mymap['e']=5;
	mymap['f']=6;
	mymap['g']=7;
	while(!mymap.empty()){
		cout<<(*mymap.begin()).first<<":"<<(*mymap.begin()).second<<endl;
		cout<<mymap.begin()->first<<":"<<mymap.begin()->second<<endl;
		mymap.erase(mymap.begin());
	}
}

运行结果

a:1
a:1
b:2
b:2
c:3
c:3
d:4
d:4
e:5
e:5
f:6
f:6
g:7
g:7

map::count()函数 功能:返回指定元素出现的次数
#include<iostream>
#include<map>
using namespace std;
 
int main(){
	 //map::count()函数 功能:返回指定元素出现的次数
	map<char,int>mymap;
	char c;
	mymap['a']=1;
	mymap['b']=2;
	mymap['c']=3;
	for(c='a';c<'h';c++){
		cout<<c;
		if(mymap.count(c)>0)
		  cout<<"  is an elements of mymap"<<endl;
		else
		  cout<<"  is not an elements of mymap"<<endl;
	} 
}

运行结果

a  is an elements of mymap
b  is an elements of mymap
c  is an elements of mymap
d  is not an elements of mymap
e  is not an elements of mymap
f  is not an elements of mymap
g  is not an elements of mymap
map::size()函数 功能:返回map中元素的个数
map::swap()函数 功能:交换两个map
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::swap()函数 功能:交换两个map
    //map::size()函数 功能:返回map中元素的个数 
	map<char,int>foo;
	map<char,int>bar;
	map<char,int>::iterator it;
	foo['x']=1;
	foo['y']=2;
	foo['z']=3;
	bar['a']=4;
	bar['b']=5;
	cout<<"before swap foo contains:"<<endl;	
	for(it=foo.begin();it!=foo.end();it++){
		cout<<(*it).first<<"->"<<(*it).second<<endl; 
	}
	cout<<"size of foo: "<<foo.size()<<endl; 
	cout<<"before swap bar contains:"<<endl;	
	for(it=bar.begin();it!=bar.end();it++){
		cout<<(*it).first<<"->"<<(*it).second<<endl; 
	}
	cout<<"size of bar: "<<bar.size()<<endl;
	foo.swap(bar);
	cout<<"after swap foo contains:"<<endl;	
	for(it=foo.begin();it!=foo.end();it++){
		cout<<(*it).first<<"->"<<(*it).second<<endl; 
	}
	cout<<"size of foo: "<<foo.size()<<endl; 
	cout<<"after swap bar contains:"<<endl;	
	for(it=bar.begin();it!=bar.end();it++){
		cout<<(*it).first<<"->"<<(*it).second<<endl; 
	}
		cout<<"size of bar: "<<bar.size()<<endl;
}

运行结果

before swap foo contains:
x->1
y->2
z->3
size of foo: 3
before swap bar contains:
a->4
b->5
size of bar: 2
after swap foo contains:
a->4
b->5
size of foo: 2
after swap bar contains:
x->1
y->2
z->3
size of bar: 3

map::get_allocator()函数 功能:返回map的配置器
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::get_allocator()函数 功能:返回map的配置器
	int psize;
	map<char,int>mymap;
	pair<const char,int>* p;
	//allocate an array of 5 elements using mamap's allocate
	p=mymap.get_allocator().allocate(5);
	psize=(int)sizeof(map<char,int>::value_type)*5;
	cout<<"the allocated array has a size of  "<<psize<<"  bytes"<<endl;
	mymap.get_allocator().deallocate(p,5);
}

运行结果

the allocated array has a size of  40  bytes
may::key_comp()函数 功能:返回比较元素key的函数
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //may::key_comp()函数 功能:返回比较元素key的函数
	map<char,int>mymap;
	map<char,int>::key_compare mycomp;
	map<char,int>::iterator it;
	char highest;
	mycomp=mymap.key_comp();
	
	mymap['a']=1; 
	mymap['b']=3; 
	mymap['c']=4; 
	mymap['d']=5; 
	mymap['e']=2; 
	cout<<"mymap contains:"<<endl;
	highest=mymap.rbegin()->first;//key value of last element
	it=mymap.begin();
	do{
		cout<<(*it).first<<"->"<<(*it).second<<endl;
	}while(mycomp((*it++).first,highest));
	cout<<endl;
}

运行结果

mymap contains:
a->1
b->3
c->4
d->5
e->2


map::value_comp()函数 功能:返回比较元素value的函数
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::value_comp()函数 功能:返回比较元素value的函数
	map<char,int>mymap;
	map<char,int>::iterator it;
	pair<char,int>highest;
	mymap['x']=2000;
	mymap['y']=2001;
	mymap['z']=2002;
	cout<<"mymap contains:"<<endl;
	highest=*mymap.rbegin();//last element
	it=mymap.begin();
	do{
		cout<<(*it).first<<"->"<<(*it).second<<endl;
		
	}while(mymap.value_comp()(*it++,highest));
}

运行结果

mymap contains:
x->2000
y->2001
z->2002
map::lower_bound()函数 功能:返回键值大于等于(>=)给定元素的第一个位置
map::upper_bound()函数 功能:返回键值大于(>)给定元素的第一个位置
#include<iostream>
#include<map>
using namespace std;
 
int main(){
    //map::lower_bound()函数 功能:返回键值大于等于(>=)给定元素的第一个位置
    //map::upper_bound()函数 功能:返回键值大于(>)给定元素的第一个位置 
	map<char,int>mymap;
	map<char,int>::iterator it,itlow,itup;
	mymap['a']=20;
	mymap['b']=40;
	mymap['c']=60;
	mymap['d']=80;
	mymap['e']=100;
	mymap['f']=120;
	mymap['g']=140;
	itlow=mymap.lower_bound('b');//itlow points to b
	itup=mymap.upper_bound('d');//itup points to e (not d!)
	mymap.erase(itlow,itup);//erases [itlow,itup)
	//print content:
	for(it=mymap.begin();it!=mymap.end();it++){
		cout<<(*it).first<<"->"<<(*it).second<<endl;
	} 
}

运行结果

a->20
e->100
f->120
g->140

map::max_size()函数 功能:返回可以容纳的最大元素个数
#include<iostream>
#include<map>
using namespace std;
    
int main(){
	//map::max_size()函数 功能:返回可以容纳的最大元素个数 
	map<int,int>mymap;
	if(mymap.max_size()>1000){
		for(int i=0;i<10;i++)mymap[i]=1;
		cout<<"the map contains 10 elements \n";
	}
	else cout<<"the map could not hold 1000 elements\n";
	cout<<"mymap contains: ";
	map<int,int>::iterator it;
	for(it=mymap.begin();it!=mymap.end();it++){
		cout<<(*it).first<<"   "<<(*it).second<<endl; 
   }
}

运行结果

the map contains 10 elements
mymap contains: 0   1
1   1
2   1
3   1
4   1
5   1
6   1
7   1
8   1
9   1

map::equal_range()函数 功能:返回map中与给定值相等的上下限的两个迭代器
#include<iostream>
#include<map>
using namespace std;
    
int main(){
	//map::equal_range()函数 功能:返回map中与给定值相等的上下限的两个迭代器 
	//左闭右开 
	map<char,int>mymap;
	pair<map<char,int>::iterator,map<char,int>::iterator>ret;
	mymap['a']=1;
	mymap['b']=2;
	mymap['e']=4;
	mymap['c']=3;
	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;
}

运行结果

lower bound points to : b ->2
upper bound points to : c ->3
multimap::count()函数 与map::count()有区别
#include<iostream>
#include<map>
using namespace std;

int main(){
	//multimap::count()函数 与map::count()有区别 
	map<char,int>mymap;
	mymap['a']=1;
	mymap['a']=2;
	mymap['a']=3;
	cout<<"map count 'a'"<<mymap.count('a')<<endl;//只输出1 
	
	multimap<char,int>mymultim;
	mymultim.insert(pair<char,int>('a',1));
	mymultim.insert(pair<char,int>('a',2));
	mymultim.insert(pair<char,int>('a',3));
	cout<<"multimap count 'a'"<<mymultim.count('a')<<endl;//输出3 
	
	
	multimap<char,int>mymm;
	multimap<char,int>::iterator it;
	char c;
	mymm.insert(pair<char,int>('x',1));
	mymm.insert(pair<char,int>('y',2));
	mymm.insert(pair<char,int>('y',3));
	mymm.insert(pair<char,int>('y',4));
	mymm.insert(pair<char,int>('z',5));
	mymm.insert(pair<char,int>('z',6));
	mymm.insert(pair<char,int>('y',1));
	mymm.insert(pair<char,int>('z',1));
	for(c='x';c<='z';c++){
		cout<<"there are "<<(int)mymm.count(c);
		cout<<" elements with key  "<<c<<"  :";
		for(it=mymm.equal_range(c).first;it!=mymm.equal_range(c).second;++it)
		  cout<<" "<<(*it).second;
		cout<<endl;
	}

}

运行结果

map count 'a'1
multimap count 'a'3
there are 1 elements with key  x  : 1
there are 4 elements with key  y  : 2 3 4 1
there are 3 elements with key  z  : 5 6 1

参考文献:《The Annotated STL Sources》侯捷

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值