说明:代码均省略其输出验证步骤
[1] map的插入元素
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
#include<iterator>
#include<set>
#include<map>//map 和 multimap
#include<string>
#include<utility>
using namespace std;
int main()
{
//m按照键值从小到大排序,键不能重复,值可以重复
map<int,string> m;
m[10]="alice";
m[15]="lisan";
//插入方法一:输出时会偷偷在m里面插入键值对
cout<<m[10]<<endl;
cout<<m[-200]<<endl;//此时插入键为-200,值为空字符
//插入方式二
m.insert(make_pair(-100,"zhaosi"));
//插入方法三
m.insert(pair<int,string>(-300,"wangwu"));
//插入方法四
m.insert(map<int,string>::value_type(-400,"wangwu"));
//插入方法五 C++11标准
m.insert({-500,"lisan"});
m.insert({{-700,"lisan"},{-600,"lisan"}});
//输出
for(map<int,string>::iterator p=m.begin();p!=m.end();p++)
{
printf("key:%5d value:%10s\n",p->first,p->second.c_str());
}
return 0;
}
[2]在定义的初步就给出map中的键值对
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
#include<iterator>
#include<set>
#include<map>//map 和 multimap
#include<string>
#include<utility>
using namespace std;
int main()
{
//定义的时候给出map里面的键值对
pair<int, string> a(30,"lisan");
pair<int, string> b(40,"wangwu");
pair<int, string> c(50,"zhaosi");
vector<pair<int,string>> a1;
a1.push_back(a);
a1.push_back(b);
a1.push_back(c);
map<int, string> m(a1.begin(),a1.end());
//C++11
map<int,string> m1={{1,"a"},{2,"b"},{3,"c"}};
//拷贝构造函数也可以用
//【注意】map里插入已有键的键值对的时候,新插入失败,保留原有键值对。multimap则不会失败
return 0;
}
[3].empty(), .size(), .count(), .find(), upper_bound()和lower_bound
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
#include<iterator>
#include<set>
#include<map>//map 和 multimap
#include<string>
#include<utility>
using namespace std;
int main()
{
//m.empty()
map<int,string> m;
m[1]="lisan";
m[2]="zhaosi";
cout<<m.empty()<<endl;
//m.size()
cout<<m.empty()<<endl;
//m.count()查找某个键的个数
cout<<m.count(1)<<endl;
//迭代子find()
map<int,string>::iterator p1=m.find(1);
cout<<p1->first<<" "<<p1->second.c_str()<<endl;
//p可以用于键值对的删除
m.erase(p1);
//multiset中间删除同一个键的多个元素:lower_bound 和 upper_bound
multimap<int,string> mm;
mm.insert(make_pair(1,"Der Tod"));
mm.insert(make_pair(1,"Elisabeth"));
mm.insert(make_pair(1,"Rudolf"));
mm.insert(make_pair(2,"Elisabeth"));
//某一个键全部去掉 lower_bound和upper_bound
multimap<int,string>::iterator p2=mm.lower_bound(1);
multimap<int,string>::iterator p3=mm.upper_bound(1);
//mm.erase(p2,p3);
//以上操作也可以用equal_range替代
pair<multimap<int,string>::iterator,multimap<int,string>::iterator> pp;
pp=mm.equal_range(1);
mm.erase(pp.first, pp.second);
//将所有的键值都输出(p1和p2做循环)
return 0;
}
[5]应用:删除某特定值的所有键值对
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
#include<iterator>
#include<set>
#include<map>//map 和 multimap
#include<string>
#include<utility>
using namespace std;
int main()
{
//删除某特定值的所有键值对
multimap<int,string> mm;
mm.insert(make_pair(1,"Der Tod"));
mm.insert(make_pair(1,"Elisabeth"));
mm.insert(make_pair(1,"Rudolf"));
mm.insert(make_pair(2,"Elisabeth"));
for(multimap<int,string>::iterator p=mm.begin();p!=mm.end();)
{
if(p->second=="Elisabeth")
{
mm.erase(p++);
continue;
}
else
{
p++;
}
}
return 0;
}
[6]例子:三维的点对应按原点对称的点
#include<iostream>
#include<map>
#include<string>
#include<utility>
#include<vector>
using namespace std; //map
class Point
{
public:
Point(int _x, int _y, int _z)
{
x = _x;
y = _y;
z = _z;
}
Point()
{
}
bool operator< (const Point &p) const
{
return this->x < p.x;
}
void ShowPoint() const
{
printf("x=%4d y=%04d z=%04d", x, y, z);
}
int x;
int y;
int z;
};
int main()
{
Point a(10, 30, 50);
Point b(-5, 50, 70);
Point c(0, 0, 0);
map<Point,Point> m;
m[a] = Point(-a.x, -a.y, -a.z);
m[b] = Point(-b.x, -b.y, -b.z);
m[c] = Point(-c.x, -c.y, -c.z);
for (map<Point, Point>::iterator p = m.begin();p != m.end();p++)
{
(p->first).ShowPoint();
cout << " ";
(p->second).ShowPoint();
cout << endl;
}
return 0;
}