C++ Map用法详解

用法汇总

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 

用法示例

// 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;

转载于:https://www.cnblogs.com/p4759521/articles/5898914.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值