#include<iostream>
#include<map>
using namespace std;
//map容器:
/*
Map的特性是,所有元素都会根据元素的键值自动排序。
Map所有的元素都是pair,同时拥有实值和键值,
pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。
*/
/*
map构造函数
map<T1, T2> mapTT;//map默认构造函数:
map(const map &mp);//拷贝构造函数
*/
/*
map赋值操作
map& operator=(const map &mp);//重载等号操作符
swap(mp);//交换两个集合容器
*/
void test01()
{
map<int, int> m;
//插入:
//插入方式4种:
//第一种:
m.insert(pair < int, int>(1, 10));
//第二种:(推荐)
m.insert(make_pair(2, 20));
//第三种:
m.insert(map<int, int>::value_type(3, 40));
//第四种:
m[4] = 40;
//对于第4种插入方式,会有不安全的隐患;
//可以通过这种方式去访问value;
//打印:
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key=" << it->first << " value=" << it->second << endl;;
}
cout << endl;
}
/*
map大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
map删除操作
clear();//删除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(keyElem);//删除容器中key为keyElem的对组。
map查找操作
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end();
count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/
void test02()
{
map<int, int> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.erase(1);
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key=" << it->first << " value=" << it->second << endl;;
}
cout << endl;
map<int, int>::iterator pos = m.find(1);
if (pos != m.end()) {
cout << "找到了,value是:" << pos->second << endl;
}
else {
cout << "未找到" << endl;
}
pos = m.find(2);
if (pos != m.end()) {
cout << "找到了,value是:" << pos->second << endl;
}
else {
cout << "未找到" << endl;
}
//count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
int num = m.count(2);
cout << "num= " << num << endl;
/*
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/
map<int, int >::iterator it_1 = m.lower_bound(3);
if (it_1 != m.end()) {
cout << "符合要求是值是: " << it_1->second << endl;
}
map<int, int>::iterator it_2 = m.upper_bound(3);
if (it_2 != m.end()) {
cout << "符合要求是值是: " << it_2->second << endl;
}
pair<map<int,int>::iterator, map<int,int>::iterator> it_3 = m.equal_range(3);
if (it_3.second!= m.end()) {
cout << "下限是:" << it_3.first->second;//first是下限;
cout << " 上限是:" << it_3.second->second;//second是上限;
cout << endl;
}
}
//指定排序规则:
//仿函数:
class myCompare
{
public:
bool operator()(const int & val1, const int & val2)
{
return val1 > val2;
}
};
void test03()
{
map<int, int,myCompare> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key=" << it->first << " value=" << it->second << endl;;
}
cout << endl;
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}