map

  1. Map 的特性是,所有元素都会根据元素的键值自动被排序,map的所有元素都是pair,同时拥有实值(value)和键值(key)Pair的第一元素被视为键值,第二元素被视为实值。Map 不允许两个元素拥有相同的键值。

 

.       pair<string,int> pairTemp(string(“A”),5);             //pair的构造函数

        6.iterator insert(iterator position, pairTemp);         //pairTemp 插入到map

        7.void erase(iterator position);                               //删除指定位置上的 map 元素

        8.size_type count(键值);                                        //判断该键值的Map 元素是否存在

        9.size_type size();                                                   //返回map 中的元素的个数

        10.iterator lower_bound(键值);                            //返回该键值或者大于该键值的map 迭代器

        11.iterator upper_bound(键值);                         //返回大于该键值的map 的迭代器    

 

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;


void Show(pair<char,int> pr)
{
	cout << pr.first << " " << pr.second << endl;
}

int main()
{
	map<char,int> mp;

	mp['E'] = 100;
	mp['F'] = 200;
	mp['A'] = 300;
	mp['D'] = 400;
	mp['B'] = 500;
	mp['C'] = 600;

	cout << mp['A'] << endl;


	/*map<char,int>::iterator ite = mp.begin();
	while(ite != mp.end())
	{
		cout << ite->first << " " << ite->second << endl;
		++ite;
	}
	cout << "===================================" << endl;*/


	::for_each(mp.begin(),mp.end(),&Show);
	cout << "===================================" << endl;


	map<char,int>::iterator itePos = mp.find('C');
	mp.erase(itePos);   //  删除

	::for_each(mp.begin(),mp.end(),&Show);
	cout << "===================================" << endl;

	pair<char,int> pr('C',123);
	mp.insert(pr);        //  插入

	::for_each(mp.begin(),mp.end(),&Show);
	cout << "===================================" << endl;

	cout << mp.count('C') << endl;    // 统计个数

	::for_each(mp.begin(),mp.end(),&Show);
	cout << "===================================" << endl;

	cout << mp.size() << endl;

	//itePos = mp.lower_bound('C');    // 返回该键值或者大于该键值的map 的迭代器
	itePos = mp.upper_bound('D');     //  返回大于该键值的map 的迭代器

	cout << itePos->first << " " << itePos->second << endl;

	//itePos->first = 'k';
	//itePos->second = 45566;

	system("pause");
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值