第十一章關聯容器

11.1,使用關聯容器


ex2???


ex3,4

string strip(string &str) {
    for (auto &ch : str) ch = tolower(ch);
    str.erase(remove_if(str.begin(), str.end(), ispunct), str.end());
    return str;
}
map<string, int> counts;
while (cin>>str){
    counts[strip(str)]++;
}

11.3
set的元素,map的second元素都是const的。所以set的迭代器是const的:set<>::const_iterator類型。

對一個map使用一個不在關聯容器的元素做下標[],會添加一個具有此關鍵字的元素。如用at(),則會拋出異常

發佈了57 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章