C++ map 的使用

参照:http://blog.csdn.net/allovexuwenqiang/article/details/5686583

 Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!
1. map最基本的构造函数;

map<string , int >mapstring;         
map<int ,string >mapint;
map<sring, char>mapstring;         
map< char ,string>mapchar;
map<char ,int>mapchar;           
map<int ,char >mapint;

结果:


2. map添加数据;

//2.删除map中的元素
	cout<<endl<<"2.删除map中的元素4"<<endl;
	maplive_iter=maplive.find(4);
	if(maplive_iter==maplive.end())
		cout<<"we do not find 4 "<<endl;
	else
	{
		maplive.erase(maplive_iter);  //delete hai;
		cout<<"we find 4 and delete it!"<<endl;
	}

	for (maplive_iter=maplive.begin();maplive_iter !=maplive.end();maplive_iter++)
	{
		cout<<maplive_iter->first<<": "<<maplive_iter->second<<". ";
	}
	cout<<endl;
结果:

3,map中元素的查找与删除:

   find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 

//2.删除map中的元素
	maplive_iter=maplive.find(4);
	if(maplive_iter==maplive.end())
		cout<<"we do not find 4 "<<endl;
	else
	{
		maplive.erase(maplive_iter);  //delete hai;
		cout<<"we find 4 and delete it!"<<endl;
	}

	cout<<endl<<"2.删除map中的元素4"<<endl;
	for (maplive_iter=maplive.begin();maplive_iter !=maplive.end();maplive_iter++)
	{
		cout<<maplive_iter->first<<": "<<maplive_iter->second<<". ";
	}
	cout<<endl;

结果:


4,map中 swap的用法:

  Map中的swap不是一个容器中的元素交换,而是两个容器交换;

// 3.map中 swap的用法:

	// Map中的swap不是一个容器中的元素交换,而是两个容器交换;
	// For example:
	cout<<endl<<"3.map中 swap的用法:"<<endl;
	map <int, int> m1, m2, m3;
	map <int, int>::iterator m1_Iter;
	map <int, int>::iterator m2_Iter;
	map <int, int>::iterator m3_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:"<<endl;
	for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
		cout<<m1_Iter->first<<": "<< m1_Iter->second<<". ";
	cout<< endl;

	cout << "The original map m2 is:"<<endl;
	for ( m2_Iter = m2.begin( ); m2_Iter != m2.end( ); m2_Iter++ )
		cout<<m2_Iter->first<<": "<< m2_Iter->second<<". ";
	cout<< endl;

	cout << "The original map m3 is:"<<endl;
	for ( m3_Iter = m3.begin( ); m3_Iter != m3.end( ); m3_Iter++ )
		cout<<m3_Iter->first<<": "<< m3_Iter->second;
	cout<<endl<<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 << "m1.swap( m2 ); map m1 is:"<<endl;
	for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
		cout<<m1_Iter->first<<": "<< m1_Iter->second<<" ";
	cout<<endl;

	cout << "m1.swap( m2 ); map m2 is:"<<endl;
	for ( m2_Iter = m2.begin( ); m2_Iter != m2.end( ); m2_Iter++ )
		cout<<m2_Iter->first<<" "<< m2_Iter->second<<" ";
	cout<<endl;
	// This is the specialized template version of swap
	swap( m1, m3 );

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

结果:


5.map的sort问题:
  Map中的元素是自动按key升序排序,所以不能对map用sort函数:

// 	4.map的sort问题:
	// 		Map中的元素是自动按key升序排序,所以不能对map用sort函数:
	// 		For example:
	map <int, int> ms1;
	map <int, int>::iterator ms1_Iter;

	ms1.insert ( pair <int, int>  ( 1, 20 ) );
	ms1.insert ( pair <int, int>  ( 4, 40 ) );
	ms1.insert ( pair <int, int>  ( 3, 60 ) );
	ms1.insert ( pair <int, int>  ( 2, 50 ) );
	ms1.insert ( pair <int, int>  ( 6, 40 ) );
	ms1.insert ( pair <int, int>  ( 7, 30 ) );

	cout<<endl<<"4.map的sort问题:"<<endl;
	cout << "The original map m1 is:"<<endl;
	for ( ms1_Iter = ms1.begin( ); ms1_Iter != ms1.end( ); ms1_Iter++ )
		cout <<  ms1_Iter->first<<" "<<ms1_Iter->second<<endl;
结果:

附录:整个程序

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
	//1.向map添加数据;
	map<int,string> maplive;  
	//几种向map中的插入添加操作
	maplive.insert(pair<int,string>(1,"aclive"));
	maplive.insert(map<int,string>::value_type(3,"hai"));
	maplive[4]="April";
	map <int, string>::iterator maplive_iter;
	cout<<"1.向map添加数据"<<endl;
	for (maplive_iter=maplive.begin();maplive_iter !=maplive.end();maplive_iter++)
	{
		cout<<maplive_iter->first<<": "<<maplive_iter->second<<". ";
	}
	cout<<endl;

	//2.删除map中的元素
	cout<<endl<<"2.删除map中的元素4"<<endl;
	maplive_iter=maplive.find(4);
	if(maplive_iter==maplive.end())
		cout<<"we do not find 4 "<<endl;
	else
	{
		maplive.erase(maplive_iter);  //delete hai;
		cout<<"we find 4 and delete it!"<<endl;
	}

	for (maplive_iter=maplive.begin();maplive_iter !=maplive.end();maplive_iter++)
	{
		cout<<maplive_iter->first<<": "<<maplive_iter->second<<". ";
	}
	cout<<endl;

	// 3.map中 swap的用法:
	// 		Map中的swap不是一个容器中的元素交换,而是两个容器交换;
	// 		For example:

	cout<<endl<<"3.map中 swap的用法:"<<endl;
	map <int, int> m1, m2, m3;
	map <int, int>::iterator m1_Iter;
	map <int, int>::iterator m2_Iter;
	map <int, int>::iterator m3_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:"<<endl;
	for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
		cout<<m1_Iter->first<<": "<< m1_Iter->second<<". ";
	cout<< endl;

	cout << "The original map m2 is:"<<endl;
	for ( m2_Iter = m2.begin( ); m2_Iter != m2.end( ); m2_Iter++ )
		cout<<m2_Iter->first<<": "<< m2_Iter->second<<". ";
	cout<< endl;

	cout << "The original map m3 is:"<<endl;
	for ( m3_Iter = m3.begin( ); m3_Iter != m3.end( ); m3_Iter++ )
		cout<<m3_Iter->first<<": "<< m3_Iter->second;
	cout<<endl<<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 << "m1.swap( m2 ); map m1 is:"<<endl;
	for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
		cout<<m1_Iter->first<<": "<< m1_Iter->second<<" ";
	cout<<endl;

	cout << "m1.swap( m2 ); map m2 is:"<<endl;
	for ( m2_Iter = m2.begin( ); m2_Iter != m2.end( ); m2_Iter++ )
		cout<<m2_Iter->first<<" "<< m2_Iter->second<<" ";
	cout<<endl;
	// This is the specialized template version of swap
	swap( m1, m3 );

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


	// 	4.map的sort问题:
	// 		Map中的元素是自动按key升序排序,所以不能对map用sort函数:
	// 		For example:
	map <int, int> ms1;
	map <int, int>::iterator ms1_Iter;

	ms1.insert ( pair <int, int>  ( 1, 20 ) );
	ms1.insert ( pair <int, int>  ( 4, 40 ) );
	ms1.insert ( pair <int, int>  ( 3, 60 ) );
	ms1.insert ( pair <int, int>  ( 2, 50 ) );
	ms1.insert ( pair <int, int>  ( 6, 40 ) );
	ms1.insert ( pair <int, int>  ( 7, 30 ) );

	cout<<endl<<"4.map的sort问题:"<<endl;
	cout << "The original map m1 is:"<<endl;
	for ( ms1_Iter = ms1.begin( ); ms1_Iter != ms1.end( ); ms1_Iter++ )
		cout <<  ms1_Iter->first<<" "<<ms1_Iter->second<<endl;

	system("pause");
	return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值