c++ map的使用方法
来源:互联网 发布:python将变量写入txt 编辑:程序博客网 时间:2024/06/05 12:48
map类型:
map是键——值对的集合,map类型通常可以理解为关联数组,可使用键作为下表来获取一个值,正如内置数组一样。而关联的本质在于元素的值与某个特定的键相连,而并非通过元素在数组中的位置来获取。
/************************************************************************
*
* Map的特点: 1、存储Key-value对
* 2、支持快速查找,查找的复杂度基本是Log(N)
* 3、快速插入,快速删除,快速修改记
*
/************************************************************************/
#include <stdio.h>
#pragma warning(disable:4786)
#include <string>
#include <map> //包含头文件
using namespace std;
//输出map中的记录
#define PRINTMAP(iterSuffix, mapName)\
{\
PRJ_MAP_STRING2INT_WORDREC::iterator iter##iterSuffix = mapName##.begin();\
for (; iter##iterSuffix != mapName##.end(); ++iter##iterSuffix)\
{\
printf("\"%s\" -> total: %d\n", iter##iterSuffix->first.c_str(), iter##iterSuffix->second);\
}\
printf("\n");\
}
/************************************************************************
* 数据类型定义
/************************************************************************/
typedef map<string, int> PRJ_MAP_STRING2INT_WORDREC;//定义map类型的别名
/************************************************************************/
/* 全局函数
/************************************************************************/
void main()
{
map<string, int> mapWordRecPrep; /* 定义map类型的变量 */
PRJ_MAP_STRING2INT_WORDREC mapWordRecVerb; /* 用别名定义map类型的变量 */
//插入记录
mapWordRecPrep["the"] = 100; /* 数组方式 */
mapWordRecPrep["so"] = 50;
mapWordRecVerb["find"] = 1;
mapWordRecVerb["seen"] = 2;
mapWordRecVerb["jump"] = 3;
mapWordRecVerb["swim"] = 4;
mapWordRecVerb.insert(map<string, int>::value_type("look", 5)); /* value_type方式 */
mapWordRecVerb.insert(pair<string, int>("walk", 6)); /* pair方式 */
/* value_type和pair方式不出现覆盖现象 */
printf("Insert method: value_type\n");
pair<map<string, int>::iterator, bool> inserted;
inserted = mapWordRecVerb.insert(map<string, int>::value_type("walk", 7));
printf("%s\n", true == inserted.second ? "Insert success!" : "Insert failed!");
PRINTMAP(Ver, mapWordRecVerb);
/* 数组方式出现覆盖现象*/
printf("Insert method: array\n");
mapWordRecVerb["walk"] = 7;
PRINTMAP(Ver, mapWordRecVerb);
//查找记录
map<string, int>::iterator iter = mapWordRecPrep.find("so");
printf("%s\n", iter == mapWordRecPrep.end() ? "Not find!" : "Find!");
//删除记录
map<string, int>::iterator iterV = mapWordRecVerb.find("seen");
if (iterV != mapWordRecVerb.end())
{
mapWordRecVerb.erase(iterV); /* 迭代器方式删除 */
printf("\nDelete word \"seen\" done!\n");
PRINTMAP(Verb, mapWordRecVerb);
}
int n = mapWordRecVerb.erase("swim"); /* 关键字方式删除 */
printf("\nDelete word \"swim\" done!\n");
PRINTMAP(Verb, mapWordRecVerb);
mapWordRecVerb.erase(mapWordRecVerb.begin(), mapWordRecVerb.end()); /* 成片删除 */
printf("\nDelete all word done!\n");
PRINTMAP(Verb, mapWordRecVerb);
//map的其它函数
if (mapWordRecVerb.empty() && 0 == mapWordRecVerb.size()) /* 判断map是否为空 */
{
printf("mapWordRecVerb
*
* Map的特点: 1、存储Key-value对
* 2、支持快速查找,查找的复杂度基本是Log(N)
* 3、快速插入,快速删除,快速修改记
*
/************************************************************************/
#include <stdio.h>
#pragma warning(disable:4786)
#include <string>
#include <map> //包含头文件
using namespace std;
//输出map中的记录
#define PRINTMAP(iterSuffix, mapName)\
{\
PRJ_MAP_STRING2INT_WORDREC::iterator iter##iterSuffix = mapName##.begin();\
for (; iter##iterSuffix != mapName##.end(); ++iter##iterSuffix)\
{\
printf("\"%s\" -> total: %d\n", iter##iterSuffix->first.c_str(), iter##iterSuffix->second);\
}\
printf("\n");\
}
/************************************************************************
* 数据类型定义
/************************************************************************/
typedef map<string, int> PRJ_MAP_STRING2INT_WORDREC;//定义map类型的别名
/************************************************************************/
/* 全局函数
/************************************************************************/
void main()
{
map<string, int> mapWordRecPrep; /* 定义map类型的变量 */
PRJ_MAP_STRING2INT_WORDREC mapWordRecVerb; /* 用别名定义map类型的变量 */
//插入记录
mapWordRecPrep["the"] = 100; /* 数组方式 */
mapWordRecPrep["so"] = 50;
mapWordRecVerb["find"] = 1;
mapWordRecVerb["seen"] = 2;
mapWordRecVerb["jump"] = 3;
mapWordRecVerb["swim"] = 4;
mapWordRecVerb.insert(map<string, int>::value_type("look", 5)); /* value_type方式 */
mapWordRecVerb.insert(pair<string, int>("walk", 6)); /* pair方式 */
/* value_type和pair方式不出现覆盖现象 */
printf("Insert method: value_type\n");
pair<map<string, int>::iterator, bool> inserted;
inserted = mapWordRecVerb.insert(map<string, int>::value_type("walk", 7));
printf("%s\n", true == inserted.second ? "Insert success!" : "Insert failed!");
PRINTMAP(Ver, mapWordRecVerb);
/* 数组方式出现覆盖现象*/
printf("Insert method: array\n");
mapWordRecVerb["walk"] = 7;
PRINTMAP(Ver, mapWordRecVerb);
//查找记录
map<string, int>::iterator iter = mapWordRecPrep.find("so");
printf("%s\n", iter == mapWordRecPrep.end() ? "Not find!" : "Find!");
//删除记录
map<string, int>::iterator iterV = mapWordRecVerb.find("seen");
if (iterV != mapWordRecVerb.end())
{
mapWordRecVerb.erase(iterV); /* 迭代器方式删除 */
printf("\nDelete word \"seen\" done!\n");
PRINTMAP(Verb, mapWordRecVerb);
}
int n = mapWordRecVerb.erase("swim"); /* 关键字方式删除 */
printf("\nDelete word \"swim\" done!\n");
PRINTMAP(Verb, mapWordRecVerb);
mapWordRecVerb.erase(mapWordRecVerb.begin(), mapWordRecVerb.end()); /* 成片删除 */
printf("\nDelete all word done!\n");
PRINTMAP(Verb, mapWordRecVerb);
//map的其它函数
if (mapWordRecVerb.empty() && 0 == mapWordRecVerb.size()) /* 判断map是否为空 */
{
printf("mapWordRecVerb
- 【C++】map容器的使用方法
- STL map的使用方法
- STL map 的使用方法
- 关于map的使用方法
- STL+map的使用方法
- map的使用方法
- c++ map的使用方法
- map的使用方法
- c++ map的使用方法
- c++ map的使用方法
- map 的使用方法
- MAP的使用方法
- c++ map的使用方法
- std::map的使用方法
- ++ map、multimap 的使用方法
- Map的使用方法
- Python:map()的使用方法
- C++STL map介绍与使用方法
- Spring.NET学习笔记1——控制反转(基础篇) Level 200
- asp.net动态页面静态化
- [Eclipse] 鼠标悬停频繁弹出提示的解决办法
- linux分区详解!
- 【java】long和Long的区别
- c++ map的使用方法
- cisco交换机查看序列号
- C++ map的使用
- s:if test循环中test的写法
- aspnetpager 分页存储过程的实现
- Excel中快速分列
- java与c/c++
- map
- java内存处理问题