1.map容器是一种关联式容器,在插入数据时会根据键值进行排序插入。
2.map容器的每一个元素都是对组,pair<key, value>,其中第一个key为键值,第二个value为实值。
3.键值不可以修改,实值可以修改,可以使用at(key)来访问实值.
4.map的迭代器不支持随机访问。
5.map的键值不允许重复,而multimap的键值可以重复.
#include<iostream>
#include<map>
#include<string>
using namespace std;
//map元素为对组<key,value>,第一个元素为键值,起到索引作用,第二个为实值
//按照键值排序,可以根据键值快速找到实值,键值不允许重复,有序对组
//迭代器不支持随机访问,只能递增++或递减--
void printMap(map<int, char>& m) {
for (map<int, char>::iterator it(m.begin()); it != m.end(); ++it)
cout << "键值:" << it->first << "\t实值:" << (*it).second << endl;
cout << endl;
}
//初始化和赋值
void test01() {
map<int, char> m; //默认构造
m.emplace(make_pair(1, 'f')); //推荐使用
m.insert(make_pair(3, 'd'));
m.emplace(pair<int, char>(2, 'e')); //对组匿名对象
m.emplace(map<int, char>::value_type(0, 'e'));
printMap(m);
map<int, char> m2(m); //拷贝构造
printMap(m2);
map<int, char> m3;
m3 = m2; //重载=赋值
printMap(m3);
}
//大小和交换
void test02() {
map<int, int> m;
m.emplace(make_pair(1, 10));
m.emplace(make_pair(2, 30));
if (!m.empty())
cout << "m的大小:" << m.size() << endl;
else cout << "m容器为空" << endl;
map<int, int> m2;
m2.emplace(make_pair(1, 1000));
m2.swap(m);
cout << "m的大小:" << m.size() << endl;
}
void printInt(map<int, int>& m) {
for (auto it = m.begin(); it != m.end(); ++it)
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
cout << endl;
}
//删除
void test03() {
map<int, int> m;
m.emplace(make_pair(1, 200));
m.emplace(make_pair(2, 300));
m.emplace(make_pair(-1, 200));
m.emplace(make_pair(0, 300));
printInt(m);
//1.按照键值进行删除
m.erase(2); //删除键值为2的对组
printInt(m);
//2.删除指定迭代器位置的对组
m.erase(++m.begin()); //删除第二个位置的对组元素
printInt(m);
m.clear(); //清空
}
//查找
void test04() {
map<int, string> m;
m.emplace(1, "亚历山大");
m.emplace(2, "拉格朗日");
m.emplace(3, "莱布尼兹");
//find(key)按照键值进行查找,找到返回该键值对应的对组的迭代器位置,
//查找失败返回终止迭代器end()
map<int, string>::iterator it = m.find(2); //查找键值为2的对组
if (it != m.end())
cout << "找到:" << it->second << endl;
else cout << "不存在该对组" << endl;
it->second = "张衡";
cout << "修改后:" << it->second << endl;
it = m.find(10); //查找键值为10的对组
if (it != m.end())
cout << "找到:" << it->second << endl;
else cout << "不存在该对组" << endl;
}
//统计count
//根据键值统计元素个数
void test05() {
//map不会插入键值相同的对组
map<string, int> m;
m.emplace(pair<string, int>("商鞅", 4000000));
m.emplace(make_pair("白起", 7800000));
m.emplace(make_pair("白起", 30000000)); //map不会插入该元组
cout << "m的大小:" << m.size() << endl;
auto it = m.find("白起");
if (it != m.end())
cout << "姓名:" << it->first << "\t工资:" << it->second << endl;
int num = m.count("商鞅"); //map里只有0或1
cout << "商鞅个数:" << num << endl;
//multimap允许插入键值相同的元组
multimap<string, int> m2;
m2.emplace(make_pair("西施", 22));
m2.emplace(make_pair("西施", 20)); //multimap会插入该元组
cout << "m2的大小:" << m2.size() << endl;
num = m2.count("西施");
cout << "西施个数:" << num << endl;
}
//类型系统内置的数据类型排序
class MyCompare {
public:
//按照键值指定为降序排序
bool operator()(int a, int b) {
return a > b;
}
};
void test06() {
map<int, int, MyCompare> m;
m.emplace(make_pair(1, 10));
m.emplace(make_pair(2, 20));
m.emplace(make_pair(3, 30));
for (auto it = m.begin(); it != m.end(); ++it)
cout << "key=" << it->first << "\tvalue=" << it->second << endl;
}
int main() {
/*cout << "**************构造和赋值***********" << endl;
test01();*/
/*cout << "**************大小和交换***********" << endl;
test02();*/
/*cout << "**************删除***********" << endl;
test03();*/
/*cout << "**************查找***********" << endl;
test04();*/
/*cout << "**************统计***********" << endl;
test05();*/
cout << "**************自带数据类型排序***********" << endl;
test06();
system("pause");
return 0;
}