map映射
map是STL的一种有序无重复的关联容器。关联容器与序列容器不同,他们的元素是按照关键字来保存和访问的。
map提供一对-(保存的是一种key-value的pair对象,key是关键字且唯一, value是关键字对应的值)的数据处理能力,由于这个特性,它完全有在我们处理一对一数据的时候,在编程上提供快速通道。
头文件#include<map>
名字空间using namespace std;
#include <iostream>
#include <map>
#include<string>
using namespace std;
int main()
{
//1、如何构造map
map<int, string>map_test1;//构造一个空的map
map<int, string>map_test2(map_test1.begin(), map_test1.end());//构造一个 map_test1
map<int, string>map_test3(map_test1);//拷贝构造
map<string, string>map_test4{ { "test1", "123" }, { "test2", "456" }, { "test3", "789" } };//键值对构造
//2、map插入三种方法:
/***************************************************************************/
#if 1
map_test1[1] = "test1";
map_test1[2] = "test2";
map_test1[3] = "test3";
//这个操作会对第一个插入的数据进行覆盖
map_test1[1] = "test4";
//正序遍历
for (map<int, string>::iterator iter = map_test1.begin(); iter != map_test1.end(); ++iter)
{
cout << "key: " <<iter->first<<",value: "<<iter->second<< endl;
}
#endif//插入方法一;下标插入
#if 0
map_test1.insert(pair<int, string>(4, "test4"));
map_test1.insert(pair<int, string>(5, "test5"));
//这个方法插入的话不会形成覆盖
map_test1.insert(pair<int, string>(4, "test6"));
//正序遍历
for (map<int, string>::iterator iter = map_test1.begin(); iter != map_test1.end(); ++iter)
{
cout << "key: " << iter->first << ",value: " << iter->second << endl;
}
#endif//插入方法二;pair插入方法
#if 0
map_test1.insert(map<int, string>::value_type(6, "test6"));
map_test1.insert(map<int, string>::value_type(7, "test7"));
map_test1.insert(map<int, string>::value_type(8, "test8"));
//这个方法插入的话不会形成覆盖
map_test1.insert(map<int, string>::value_type(6, "test9"));
//正序遍历
for (map<int, string>::iterator iter = map_test1.begin(); iter != map_test1.end(); ++iter)
{
cout << "key: " << iter->first << ",value: " << iter->second << endl;
}
#endif//插入方法三;map<int, string>::value_type插入方法
/***************************************************************************/
//3、map遍历三种方法:
/***************************************************************************/
#if 0
map<string, string>::iterator iter;
for (iter = map_test4.begin(); iter != map_test4.end(); ++iter)
{
cout << "key: " << iter->first << ",value: " << iter->second << endl;
}
#endif//遍历方法一:正向遍历器(效果是正序的,会自动排序)
#if 0
map<string, string>::reverse_iterator iter2;
for (iter2 = map_test4.rbegin(); iter2 != map_test4.rend(); ++iter2)
{
cout << "key: " << iter2->first << ",value: " << iter2->second << endl;
}
#endif//遍历方法二;反向遍历器(效果是倒序的,会自动排序)
#if 0
//下标访问key值遍历
cout << map_test4["test1"] << endl;
cout << map_test4["test3"] << endl;
//可以使用.at()访问
cout << map_test4.at("test2") << endl;
#endif//遍历方法三;key值访问(效果是只会输出value值,但是能访问指定的值,不会进行排序)
/***************************************************************************/
//4、查找并获取map中元素,三种方法
/***************************************************************************/
#if 0
//1.用count函数判断关键字是否有元素,有是1 没有是0
//是一个bool类型.
//PS:count()是用key值访问
cout << map_test1.count(1) << endl;
cout << map_test1.count(0) << endl;
#endif//查找方法一:用count函数判断关键字是否有元素
#if 0
//2.用find 返回选代器 没找到返回end()
//是一个迭代器类型
//PS:find()是用key值访问
map<int, string>::iterator it= map_test1.find(1);
if (it != map_test1.end())
{
//first访问key值
cout <<"key:"<< it->first << endl;
//second访问value值
cout <<"value:"<< it->second << endl;
}
else
{
cout << "do not find" << endl;
}
#endif//查找方法二:find遍历
#if 0
//创建一个遍历器
map<int, string>::iterator it1;
//寻找到>=的第一个元素位置
it1 = map_test1.lower_bound(3);
//寻找到>的第一个元素位置
it1 = map_test1.upper_bound(1);
#endif//查找方法三:1、lower_bound() and 2、upper_bound()
/***************************************************************************/
//5、删除元素:erase(),三种方法
/***************************************************************************/
//创建一个迭代器
map<int, string>::iterator it2;
//删除第一个元素
map_test1.erase(map_test1.begin());
//key值删除
map_test1.erase(2);
//范围删除
map_test1.erase(map_test1.begin(), map_test1.end());
/***************************************************************************/
system("pause");
return 0;
}