c++中STL中map的基本用法
具体的详见代码或者 http://www.cplusplus.com/reference/map/map/
// map的基本使用
#include "pch.h"
#include <iostream>
//map需要包含的头文件
#include <map>
using namespace std;
int main()
{
//定义一个map
std::map<int,std::string> map_test;
//map的插入方式
//1、insert方法 pair 形式
map_test.insert(std::pair<int, std:: string>(1, "one"));
map_test.insert(std::pair<int, std::string>(2, "two"));
map_test.insert(std::pair<int, std::string>(3, "three"));
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
//2、insert方法 value_type 形式
map_test.insert(map<int, string>::value_type (4, "four"));
map_test.insert(map<int, string>::value_type (5, "five"));
map_test.insert(map<int, string>::value_type (6, "six"));
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
//3、数组的形式
map_test[7] = "seven";
map_test[8] = "eight";
map_test[9] = "nine";
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
// 查看insert方法是否成功
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = map_test.insert(pair<int, string>(1, "one"));
if(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
Insert_Pair = map_test.insert(pair<int, string>(1, "one_repeat"));
if(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
//通过数组可以成功其value值
map_test[1] = "one_array";
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
//通过迭代器改变其value值
auto iter1 = map_test.begin();
iter1->second = "one_iter";
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
// 利用map的内置函数查看是否有该元素
auto iter2 = map_test.find(1);
if(iter2 != map_test.end())
{
printf("find %d--%s\n",iter2->first,(iter2->second).c_str());
}
else
{
printf("not find\n");
}
//利用数组下标直接访问存在的元素
printf(" %s %d %d \n",map_test[1].c_str(),map_test[1].size(),map_test[1].length());
//利用数组下标直接访问不存在的元素
printf(" %s %d %d \n",map_test[11].c_str(),map_test[11].size(),map_test[11].length());
//删除一个元素
auto iter3 = map_test.begin();
map_test.erase(iter3);
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
//删除存在的元素 返回值为 1
int n = map_test.erase(2);
if(n == 1)
{
printf("n %d \n",n);
}
else
{
printf("n %d \n",n);
}
//删除不存在的元素 返回值为 1
n = map_test.erase(22);
if(n == 1)
{
printf("n %d \n",n);
}
else
{
printf("n %d \n",n);
}
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
// 清空元素
#if 0
map_test.erase(map_test.begin(),map_test.end());
#else
map_test.clear();
#endif
printf("*************************************\n");
for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
{
printf("%d %s\n",iter->first,(iter->second).c_str());
}
printf("*************************************\n");
}