STL map 代码测试

#include
#include
#include
#include
using namespace std;

void fun (pair<int,char> pr)
{
cout << pr.first << " " << pr.second << endl; //first键值。second实值
}
void MapConstruct()
{
typrdef pair<int,char> in_pair;
pair<map.<int,char>::iterator,bool> pr;

map<int, char> mp;  // <\>
mp.insert(pair<int,char>(1,'a'));
pr = mp.insert(in_pair(3,'a'));  

// cout << pr.second <<endl; //插入成功,返回1,不成0 ,1
pr = mp.insert(in_pair(5,‘b’));
// cout << pr.second <<endl; // 1
pr = mp.insert(in_pair(7,‘b’));
pr = mp.insert(in_pair(9,‘b’));
pr = mp.insert(in_pair(11,‘b’));
pr = mp.insert(in_pair(13,‘b’));

map<int,char>::iterator ite = mp.begin();
ite++;
ite++;
ite++;
ite++;
mp.insert(ite, in_pair(2,'b'));  //自动排序插入 

map<int,char> mp1;
mp1.insert(in_pair(4,'d'));
mp1.insert(in_pair(6,'d'));
mp1.insert(mp.begin(), mp.end());


map<int, char> mp2(mp1);
map<int,char>  mp3(mp2.begin(),mp3.end())


for_each(mp3.begin(),mp3.end(),fun);

}

void MapOther
{
typedef pair<int,char>input_pair;
map<int,char> mp;

// mp.insert(input_pair(1,‘a’));
// mp.insert(input_pair(2,‘b’));
// mp.insert(input_pair(3,‘c’));
// mp.insert(input_pair(4,‘d’));
// mp.insert(input_pair(5,‘e’));

cout << mp.size() <<endl;
cout << mp.count(11) <<endl; //0判断有没有 
cout << mp.empty() << endl;

map<int, char>::iterator ite = mp.begin();

for(ite; ite != mp.end(); ite++)
{
	cout << ite->first << " " << ite->second << endl;
}

}

void MapDelete()
{
tyoedef pair<int, char> in_pair;
map<int,char,greater> mp; //sort()从大到小

mp.insert(in_pair(1, 'a'));  // 7 5 3 1
mp.insert(in_pair(3, 'b')); 
mp.insert(in_pair(5, 'c')); 
mp.insert(in_pair(7, 'd')); 

// if (mp.end() == mp.find(6))
// {
// cout << “ok”;
// }

cout << mp.lower_bound(3)->first << endl; //3
cout << mp.upper_bound(3)->first << endl; //5

// cout << mp.find(6)->first << endl;
// map<int, char>::iterator ite = mp.begin();
// ite++;
// ite++;

// mp.erase(ite,mp.end());// 1 2 后面删除
// mp.erase(3) //删除3

// ite = mp.begin();
map<int, char,greater>::iterator ite = mp.begin();

/* map<int, char>::iterator ite1 = mp.find(2);
cout << ite1->first << " " << ite1->second <<endl; //查找键值 */

for(ite; ite != mp.end(); ite++)
{
	cout << ite->first << " " << ite->second << endl;
}

}
int main()
{
MapOther();
MapConstruct();
MapDelete

system("pause");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值