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用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #30
- 下一篇: 深度好文