c++ map索引不存在的key可能導(dǎo)致的后果分析
今天調(diào)這個(gè)調(diào)了很久才發(fā)現(xiàn)這個(gè)問題,所以記錄以下
測試代碼
#include<bits/stdc++.h> using namespace std; int main() { map<int,int>mp_int; map<string,string>mp_string; map<char,char>mp_char; mp_int[1]=10; string a="abc",b="xzy",c="def"; mp_string[a]=b; mp_char['a']='b'; cout<<"正常索引"<<endl; for(auto &i:mp_int)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_string)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_char)cout<<i.first<<" "<<i.second<<endl; cout<<"訪問不存在的鍵"<<endl; cout<<mp_int[2]<<endl<<mp_string[c]<<endl<<mp_char['c']<<endl; cout<<"變化"<<endl; for(auto &i:mp_int)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_string)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_char)cout<<i.first<<" "<<i.second<<endl; return 0; }
OUT PUT
正常索引
1 10
abc xzy
a b
訪問不存在的鍵
0
變化
1 10
2 0
abc xzy
def
a b
c
可以發(fā)現(xiàn)不存在的key在被索引后被添加到了map中并被賦予了一個(gè)默認(rèn)值(一般的,整數(shù)為0,字符,字符串為空)
需要注意的是,只要發(fā)生了索引,就會(huì)導(dǎo)致如上錯(cuò)誤,即使他們在if語句里
#include<bits/stdc++.h> using namespace std; int main() { map<int,int>mp_int; map<string,string>mp_string; map<char,char>mp_char; mp_int[1]=10; string a="abc",b="xzy",c="def"; mp_string[a]=b; mp_char['a']='b'; cout<<"正常索引"<<endl; for(auto &i:mp_int)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_string)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_char)cout<<i.first<<" "<<i.second<<endl; cout<<"訪問不存在的鍵"<<endl; if(mp_int[2]); if(mp_string[c]==a); if(mp_char['c']); cout<<"變化"<<endl; for(auto &i:mp_int)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_string)cout<<i.first<<" "<<i.second<<endl; for(auto &i:mp_char)cout<<i.first<<" "<<i.second<<endl; return 0; }
上面的代碼會(huì)產(chǎn)生同樣的結(jié)果
當(dāng)你想要再次使用(循環(huán))這些鍵的時(shí)候就會(huì)出錯(cuò),你會(huì)使用到實(shí)際并不存在的key
避免方法是在索引前使用find或者count來判斷鍵是否存在
到此這篇關(guān)于c++ map索引不存在的key可能導(dǎo)致的后果分析的文章就介紹到這了,更多相關(guān)c++ map索引內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++ map用法總結(jié)(整理)
- C++保存HBITMAP為位圖文件的實(shí)現(xiàn)方法
- C++利用map實(shí)現(xiàn)并查集
- c++容器list、vector、map、set區(qū)別與用法詳解
- C++ map 根據(jù)value找key的實(shí)現(xiàn)
- C++中rapidjson將嵌套map轉(zhuǎn)為嵌套json的講解
- C++中rapidjson將map轉(zhuǎn)為json的方法
- C++中rapidjson組裝map和數(shù)組array的代碼示例
- C++中map和vector作形參時(shí)如何給定默認(rèn)參數(shù)?
- c++ 數(shù)據(jù)結(jié)構(gòu)map的使用詳解
相關(guān)文章
Visual Studio 2022 的安裝和創(chuàng)建C++項(xiàng)目(圖文教程)
本文主要介紹了Visual Studio 2022 的安裝和創(chuàng)建C++項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C++ Boost shared_ptr共享指針詳細(xì)講解
shared_ptr是一個(gè)標(biāo)準(zhǔn)的共享所有權(quán)的智能指針,允許多個(gè)指針指向同一個(gè)對象,定義在memory文件中,命名空間為std,這篇文章主要介紹了C++ shared_ptr使用,需要的朋友可以參考下2022-11-11C語言中全局?jǐn)?shù)組和局部數(shù)組的問題
今天同學(xué)遇到一個(gè)在C語言中全局?jǐn)?shù)組和局部數(shù)組的問題,卡了許久,我也沒有第一時(shí)間看出問題,現(xiàn)在把問題梳理一下,并給出解決方案,需要的朋友可以參考下2012-12-12詳解C++標(biāo)準(zhǔn)庫中處理正則表達(dá)式的類std::regex
std?是?C++?標(biāo)準(zhǔn)庫的命名空間,包含了大量標(biāo)準(zhǔn)的?C++?類、函數(shù)和對象,這些類和函數(shù)提供了廣泛的功能,包括輸入輸出、容器、算法、字符串處理等,這篇文章主要介紹了C++標(biāo)準(zhǔn)庫中提供的用于處理正則表達(dá)式的類std::regex,需要的朋友可以參考下2024-03-03C語言中關(guān)于scanf函數(shù)的一些問題詳解
這篇文章主要為大家介紹了C語言中關(guān)于scanf函數(shù)的一些問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12