你知道什么是map嗎?大家在接觸C++的時候是不是都有看到過map呢?那么你知道C++中如何使用map嗎?相信很多人都是非常想知道的,那么接下來的內容中就讓小編為大家介紹它的使用方法吧。/************************************************************************** Map的特點: 1、存儲Key-value對* 2、支持快速查找,查找的復雜度基本是Log(N)* 3、快速插入,快速刪除,快速修改記*/************************************************************************/#include <iostream>#include <string>#include <map> using namespace std;int main(){ map<const char*,int> m; m["a"]=1; m["b"]=6; m["c"]=9; map<const char*,int>::iterator it; it=m.begin(); const char* c =it->first; cout<<"first element is :"<<c<<endl; int i = m["c"]; while(it!=m.end()){ cout << it->first<<";"<<it->second<<endl; ++it; } cout <<"m[/"c/"]="<<i<<endl; cout <<"sizeof m:"<<m.size()<<endl; cout <<"erase m[/"c/"](1:succ 0:failed):"<<m.erase("c")<<endl; cout <<"erase m[/"c/"]:"<<m.erase("c")<<endl; cout <<"sizeof m:"<<m.size()<<endl; cout<<"m[c]="<<m["c"]<<endl; cout<<"sizeof m :"<<m.size()<<endl; return 0;}
運行結果
以上就是小編為大家帶來的C++中如何使用map的內容,想要學習的朋友們,趕快把這篇文章收入囊中吧,對你的學習是很有幫助的哦!
新聞熱點
疑難解答
圖片精選