#include<iostream>
#include<map>
#include<algorithm>
#include<functional>
using namespace std;
void MapDelete()
{
typedef pair<int, char> in_pair;
map<int, char> db;//结果默认键值排序从小到大。
//map<int, char,greater<int>> db;//结果修改为键值排序从大到小。
db.insert(in_pair(1, 'a'));
db.insert(in_pair(2, 'a'));
db.insert(in_pair(3, 'a'));
db.insert(in_pair(4, 'a'));
迭代输出
//map<int, char>::iterator ite = db.begin();
map<int, char,greater<int>>::iterator ite = db.begin();//结果修改为键值排序从大到小。
//ite++;
//ite++;
删除
db.erase(ite);//删除键值为3的值
//db.erase(3);//删除键值为3的值
db.erase(ite, db.end());//删除键值为3到尾末的值。
//ite = db.begin();//修改过后迭代器会失效,所以要重新声明。
/*for ( ite ; ite != db.end(); ite++)
{
cout << ite->first << " " << ite->second << endl;
}*/
//迭代器查找。
map<int, char>::iterator ite = db.begin();
map<int, char>::iterator ite1 = db.find(2);//查找2这个键值,查找到会返回2所在位置的迭代器。
if (db.end() == db.find(6)) //如果容器中没有6这个键值,则find返回最后一个元素end()(并不是最后一个键值,而是实际尾元素的下一个end()),运行会崩溃。
{
cout << "OK" << endl;
}
cout << ite1->first << " " << ite1->second << endl;
cout << db.lower_bound(3)->first << endl;//返回参数key的位置,该位置大于等于于key:key<=pos。
cout << db.upper_bound(3)->first << endl;//返回参数key的下一个位置,该位置大于key:key<pos。
}
int main()
{
//MapOther();
MapDelete();
system("pause");
return 0;
}
*键值不提供修改(红黑树所致),实际值可以修改,修改键值会使整个结构发生改变。