map知识整理



一:map是一种关联式容器,关联式容器分为以下几种:

set   快速查找,不允许重复值
multiset  快速查找,允许重复值
map   一对一映射,基于关键字快速查找,不允许重复值
multimap  一对多映射,基于关键字快速查找,允许重复值


map中:键值对中 键:即存放值的编号    值:存放的数据
   多个键名可以对应同一个键值,键不可以重复,值可以重复,插入多个相同的键,其值是最后一次插入的值 。

二:map容器的底层是采用红黑二叉树排序,算法速度比普通的for循环来的快。

以下整理map容器常用的用法:


#include<iostream>
#include <afx.h>
#include <map>
using namespace std;
int main()
{
	map<int,CString> mapString;

	/**********map容器插入的两种方式*************/
	mapString[1] = _T("Gaoshouqi");
	mapString.insert(pair<int,CString>(2,_T("wangyanbo")));
	/***********************************************/

	//此时将键值为2对应的名字给重写为最后一次插入的值
	mapString[2] = _T("menghui");

	map<int,CString>::iterator it;
	it = mapString.find(2);

	/*********查找元素********/
	if (it == mapString.end())
	{
		cout << _T("We not find") << endl;
	}
	else
	{
		CString str = it->second ;
		cout << str <<endl;
	}

	/*******map删除元素*******/
	it = mapString.find(1);
	if (it == mapString.end())
	{
		cout << _T("We not find") << endl;
	}
	else
	{
		mapString.erase(it);
	}

	/************map中用swap用法是多个容器之间互相交换*********/
	map <int, int> m1, m2, m3;
	map <int, int>::iterator m1_Iter;

	m1.insert ( pair <int, int>  ( 1, 10 ) );
	m1.insert ( pair <int, int>  ( 2, 20 ) );
	m1.insert ( pair <int, int>  ( 3, 30 ) );
	m2.insert ( pair <int, int>  ( 10, 100 ) );
	m2.insert ( pair <int, int>  ( 20, 200 ) );
	m3.insert ( pair <int, int>  ( 30, 300 ) );
	
	cout << "The original map m1 is:";
	for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
		cout << " " << m1_Iter->second;
	cout   << "." << endl;

	// This is the member function version of swap
	//m2 is said to be the argument map; m1 the target map
	m1.swap( m2 );

	cout << "After swapping with m2, map m1 is:";
	for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
		cout << " " << m1_Iter -> second;
	cout  << "." << endl;

	cout << "After swapping with m2, map m2 is:";

	for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
		cout << " " << m1_Iter -> second;
	cout  << "." << endl;

	// This is the specialized template version of swap
	swap( m1, m3 );

	cout << "After swapping with m3, map m1 is:";
	for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
		cout << " " << m1_Iter -> second;
	cout   << "." << endl;

	/************map中用sort排序问题*********/
	map<int, int> mapNum;
	mapNum.insert(pair<int, int>(1, 20));
	mapNum.insert(pair<int, int>(4, 30));
	mapNum.insert(pair<int, int>(2, 50));
	mapNum.insert(pair<int, int>(5, 80));
	mapNum.insert(pair<int, int>(6, 80));

	map<int, int>::iterator its; 
	for (its = mapNum.begin(); its != mapNum.end(); its++)
	{
		cout << its->first << " " << its->second << endl;
	}
	/*****编译结果*****
	1 20
	2 50
	4 30
	5 80
	6 80
	map中的键值是默认以key的升序排列,不能对map执行sort操作。
	*******************/
	cin.get();
	return 0;
}
以上例子参考http://blog.csdn.net/mingliuboy/article/details/6722780博客,表示感谢。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值