博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Map用法详解
阅读量:4303 次
发布时间:2019-05-27

本文共 3221 字,大约阅读时间需要 10 分钟。

用法汇总

insert

插入一个元素

size

获得map中元素的个数

max_size

获得map所能容纳的元素个数

count

判断是否存在某个key,存在为返回1

find

查找某个key

erase

删除指定的元素

clear

清空map

empty

判断map是否为空

begin

获取map的第一个元素,一般遍历的时候用

end

获取map的最后一个元素,一般遍历的时候用

rbegin

获取map的第一个元素,一般倒序遍历时候用

rend

获取map的最后一个元素,一般倒序遍历时候用

value_comp

Retrieves a copy of the comparison object that is used to order element values in a map.

lower_bound

Returns an iterator to the first element in a map that has a key value that is equal to or greater than that of a specified key.

upper_bound

Returns an iterator to the first element in a map that has a key value that is greater than that of a specified key.

swap

Exchanges the elements of two maps.

equal_range

Returns a pair of iterators. The first iterator in the pair points to the first element in a map with a key that is greater than a specified key. The second iterator in the pair points to the first element in the map with a key that is equal to or greater than the key.

get_allocator

Returns a copy of the allocator object that is used to construct the map.

key_comp

Returns a copy of the comparison object that used to order keys in a 

用法示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

// base define

std::map <std::string, std::string> sColleagueMap;

 

// 插入元素

sColleagueMap.insert( std::pair<std::string, std::string>("rao""haijun") );

sColleagueMap.insert( std::pair<std::string, std::string>("zhao""jingjing") );

sColleagueMap.insert( std::pair<std::string, std::string>("fan""junjie") );

 

// 访问指定元素

std::cout<<sColleagueMap.at("rao")<<std::endl;

 

// 判断map中是否包含某个元素

std::cout<<sColleagueMap.count("rao")<<std::endl;

 

// 查找map中是否包含某个元素

auto findIt = sColleagueMap.find("fan");

if(findIt != sColleagueMap.end() ){

std::cout<<"find the element:"<<findIt->first<<findIt->second<<std::endl;

}

 

// 遍历元素(不允许修改) 从前到后 C++11标准

auto sCollItConst = sColleagueMap.cbegin();

for(; sCollItConst!=sColleagueMap.cend(); ++sCollItConst){

std::cout<<sCollItConst->first<<sCollItConst->second<<std::endl;

}

 

// 遍历元素(不允许修改) 从后到前 C++11标准

auto sCollItConstr = sColleagueMap.crbegin();

for(; sCollItConstr!=sColleagueMap.crend(); ++sCollItConstr){

std::cout<<sCollItConstr->first<<sCollItConstr->second<<std::endl;

}

 

// 遍历元素(可以修改)从前到后

std::map<std::string, std::string>::iterator sCollIt = sColleagueMap.begin();

for(; sCollIt!=sColleagueMap.end(); ++sCollIt){

if(sCollIt->first == "rao"){

sCollIt->second = "yuke";

}

std::cout<<sCollIt->first<<sCollIt->second<<std::endl;

}

// 遍历元素(可以修改)从后到前

std::map<std::string, std::string>::reverse_iterator sCollItR = sColleagueMap.rbegin();

for(; sCollItR!=sColleagueMap.rend(); ++sCollItR){

if(sCollItR->first == "rao"){

sCollItR->second = "zhihao";

}

std::cout<<sCollItR->first<<sCollItR->second<<std::endl;

}

// 清空所有元素

sColleagueMap.clear();

std::cout<<sColleagueMap.size()<<std::endl;

 

// 判断map的所能hold到的最大element个数

std::cout<<"max:"<<sColleagueMap.max_size()<<std::endl;

char *pNew = new char[1024*1024*1024];

if(!pNew){

std::cout<<"malloc memory error!"<<std::endl;

}

std::cout<<"max:"<<sColleagueMap.max_size()<<std::endl;

delete[] pNew;

// 判断是否为空

std::cout<<sColleagueMap.empty()<<std::endl;

转载地址:http://esmws.baihongyu.com/

你可能感兴趣的文章
【JDBC】day03_数据库连接池
查看>>
【JDBC】day04_事务_批处理_自动主键_DAO
查看>>
有关线程调度问题
查看>>
MyEclipse注册码生成代码
查看>>
Java中Socket阻塞的原因
查看>>
【html】day01_Web概述_html工作原理_XML与HTML_乱码问题
查看>>
【html】day02_标记_注释_HTML文档类型
查看>>
【html】day03_head_文本元素_行内元素
查看>>
【html】day04_图像和超链接
查看>>
Java程序员面试中的多线程问题
查看>>
数据库连接池问题
查看>>
java.sql.SQLException的常见原因
查看>>
【html】day05_表格_表单
查看>>
【css】day01_css概述_使用CSS_CSS语法
查看>>
【css】day02_css选择器_border_box_背景
查看>>
Java复选框JCheckBox和单选按钮JRadioButton用法详解
查看>>
mysql取系统当前时间的函数
查看>>
Java中GUI相关控件常见问题详解
查看>>
【css】day03_文本格式化_表格样式_定位
查看>>
【css】day04_列表样式_显示方式_鼠标形状
查看>>