日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(chap 11)关联容器操作

發(fā)布時(shí)間:2025/3/21 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ Primer 5th笔记(chap 11)关联容器操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 添加元素insert

參數(shù)返回值
m.insert(e);e為pair 返回一個(gè)pair, first指向e的一個(gè)迭代器,second 是bool值表示是否成功插入
m.emplace(args)args是參數(shù)列表
m.insert(beg,end);b e是迭代器 返回void
m.insert(il);值的花括號(hào)列表 返回void
m.insert(iter,e);iter為輔助,e為pair 返回迭代器,指向m中具有特定鍵的元素
m.insert(iter,args);iter為輔助,args是參數(shù)列表 返回迭代器,指向m中具有特定鍵的元素

eg.

string word("dda"); //map<string, size_t> m = { { "2", 0 } };map<string, size_t> word_count; word_count.insert({ word, 1 });//在參數(shù)列表中使用花括號(hào)初始化創(chuàng)建了一個(gè)pair。 word_count.insert(make_pair(word, 1));//也可在參數(shù)列表中調(diào)用make_pair構(gòu)造pair。 word_count.insert(pair<string, size_t>(word, 1));//也可顯示構(gòu)造pair。 word_count.insert(map<string, size_t>::value_type(word, 1)); //先構(gòu)造一個(gè)pair類型,再構(gòu)造該類型的一個(gè)新對象(對象類型為pair)。map<string, size_t>::iterator a = word_count.begin(); map<string, size_t>::iterator b = ++a;cout << word_count.size() << endl; word_count.insert(a, b); word_count.insert(b, {"da", 0}); cout << word_count.size() << endl;

eg2.

向multiset或multimap添加元素 multimap<string, string> authors; authors.insert({"author1", "book1"}); authors.insert({"author1", "book2"});

2. 遍歷

std::map<int, int> word_count = { {0, 100},{ 1,200}, { 2,300 },{4,400} }; auto map_it = word_count.cbegin(); while (map_it != word_count.cend()) {...}

3. 刪除

參數(shù)返回值
m.erase(k)k為關(guān)鍵字 返回一個(gè)size_type,表示刪除元素的數(shù)量
m.erase§p是迭代器 p必須指向m中一個(gè)真實(shí)元素,返回指向p之后元素的迭代器(可以為end)
m.erase(beg,end)beg end是迭代器 返回end

4. 下標(biāo)操作

c[k]
c.at(k)
區(qū)別是什么?

5. 訪問元素

方法返回值
c.find(k)返回一個(gè)迭代器,指向第一個(gè)關(guān)鍵字為k的元素,若沒有找到則返回end
c.count(k)返回一個(gè)size_type,表示刪除元素的數(shù)量
c.lower_bound(k)返回一個(gè)迭代器,指向第一個(gè)關(guān)鍵字不小于k的元素
c.upper_bound(k)返回一個(gè)迭代器,指向第一個(gè)關(guān)鍵字大于k的元素
c.equal_range(k)返回一個(gè)迭代器pair,表示關(guān)鍵字等于k的元素范圍。若k不存在,pair的兩個(gè)成員均為end()

eg.

// map from author to title; there can be multiple titles per author multimap<string, string> authors;// add data to authors authors.insert({ "Alain de Botton", "On Love" }); authors.insert({ "Alain de Botton", "Status Anxiety" }); authors.insert({ "Alain de Botton", "Art of Travel" }); authors.insert({ "Alain de Botton", "Architecture of Happiness" }); /* authors.insert(pair<string, string>("Alain de Botton", "On Love")); authors.insert(pair<string, string>("Alain de Botton", "Status Anxiety")); authors.insert(pair<string, string>("Alain de Botton", "Art of Travel")); authors.insert(pair<string, string>("Alain de Botton", "Architecture of Happiness")); */ string search_item("Alain de Botton"); // author we'll look for auto entries = authors.count(search_item); // number of elements auto iter = authors.find(search_item); // first entry for this author /* // loop through the number of entries there are for this author while (iter != authors.end()) {//while(entries) cout << iter->second << endl; // print each title++iter; // advance to the next title--entries; // keep track of how many we've printed } */ // definitions of authors and search_item as above // beg and end denote the range of elements for this author for (auto beg = authors.lower_bound(search_item),end = authors.upper_bound(search_item);beg != end; ++beg)cout << beg->second << endl; // print each title// definitions of authors and search_item as above // pos holds iterators that denote the range of elements for this key for (auto pos = authors.equal_range(search_item);pos.first != pos.second; ++pos.first)cout << pos.first->second << endl; // print each title【引用】1. 代碼 https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/mapTest.h

總結(jié)

以上是生活随笔為你收集整理的C++ Primer 5th笔记(chap 11)关联容器操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。