转自
/*
***********************************************************************
*
* 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 is empty!\n\n " );
}
PRINTMAP(Pre, mapWordRecPrep)
mapWordRecPrep.clear(); /* 清空map */
PRINTMAP(Pre, mapWordRecPrep)
if (mapWordRecPrep.empty() && 0 == mapWordRecPrep.size())
{
printf( " mapWordRecPrep is empty!\n\n " );
}
}
*
* 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 is empty!\n\n " );
}
PRINTMAP(Pre, mapWordRecPrep)
mapWordRecPrep.clear(); /* 清空map */
PRINTMAP(Pre, mapWordRecPrep)
if (mapWordRecPrep.empty() && 0 == mapWordRecPrep.size())
{
printf( " mapWordRecPrep is empty!\n\n " );
}
}
输出结果:
Insert method: value_type
Insert failed!
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"seen" -> total: 2
"swim" -> total: 4
"walk" -> total: 6
Insert method: array
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"seen" -> total: 2
"swim" -> total: 4
"walk" -> total: 7
Find!
Delete word "seen" done!
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"swim" -> total: 4
"walk" -> total: 7
Delete word "swim" done!
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"walk" -> total: 7
Delete all word done!
mapWordRecVerb is empty!
"so" -> total: 50
"the" -> total: 100
mapWordRecPrep is empty!