C++ std map

函數(shù)模板 類似 數(shù)組有下標(biāo)一樣 可以便利的訪問

· std::map<key_type,data_type,[comparison_function]>`

定義一個(gè)學(xué)生(string)成績(char A?。隆。谩。?的map
std::map <string,char> grade_list;
grade_list["John"]='B';

if you want improve John's grades
grade_list["John"]='A';

So adding keys to a map can be done without doing anything special -- we just need to use key and it will be added automatically along with the corresponding data item.On the other hand,getting rid of an element requires calling the function erase, which is a member of the map class:

erase(key_type key_value);
for instance
grade_list.erase("John");

清空map
·grade_list.clear();·

std::map <string,char> grade_list;
grad_list["John"]='A';
if(grad_list.find("Tim")==grad_list.end())
{
  std::cout<<"Tim is not in the map!"<<endl;
}
std::map<string,char> grade_list;
grade_list["John"] ='A';
std::cout<<grade_list.begin()->first<<endl;
std::cout<<grade_list.begin(0->sencond<<endl;

好處:

  • 提供一種關(guān)聯(lián)數(shù)組
  • map 可以用iterators 訪問 first second
  • Maps are fast O(log(n)) insertion and lookup time
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容