日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C++ Map用法详解

發布時間:2025/6/17 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的C++ Map用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。