c/c++语言开发共享c/c++ 标准库 map multimap元素访问

标准库 map multimap元素访问 一,map,unordered_map下标操作 | 下标操作种类 | 功能描述 | | | | | c[k] | 返回关键字为k的元素;如果k不在c中,添加一个关键字为k的元素,并对其初始化 | | c.at(k) | 访问关键字为k的元素;若k不在c中,抛 …

标准库 map multimap元素访问

一,map,unordered_map下标操作

下标操作种类 功能描述
c[k] 返回关键字为k的元素;如果k不在c中,添加一个关键字为k的元素,并对其初始化
c.at(k) 访问关键字为k的元素;若k不在c中,抛出out_of_range异常

注意:

1,当使用使用自定义类作为key时,这个类必须重写operator<函数

2,下标操作只适用于const map,unordered_map

二,访问元素

查找元素的操作 功能描述
c.find(k) 返回一个迭代器,指向第一个关键字为k的元素,若k不在c种,则返回c.end()
c.count(k) 返回关键字等于k的元素的数量。
c.lower_bound(k) 返回一个迭代器,指向第一个关键字大于等于k的元素。若k不在c中,返回和c.upper_bound(k)相等的迭代器。
c.upper_bound(k) 返回一个迭代器,指向第一个关键字大于k的元素。若k不在c中,返回和c.lower_bound(k)相等的迭代器。
c.equal_range(k) 返回一个pair,pair里面是2个c的迭代器。first为第一个关键字等于k的迭代器,second为最后一个关键字等于k的位置的下一个位置的迭代器。若未找到,则pair的2个成员都等于c.end()

小例子向导:

程序块 功能描述
test1 map的下标操作
test2 map 用自定义类型的下标操作
test3 map的查找
test4 multimap的查找

小例子:

#include <iostream> #include <map> #include <unordered_map> #include <set> #include <vector>  using namespace std;  class test{ public:   test(int d = 0):data(d){}   bool operator<(const test& s)const{     return s.data < data;   }   const int& getdata()const{     return data;   } private:   int data; }; int main(){   //test1 map的下标操作                                                            /*                                                                               map<string,int> smap{{"aa",12},{"bb",10}};                                       unordered_map<int, int> imap{{1,11},{2,22}};                                     map<string,int>::mapped_type m1 = smap["aa"];//m1为int                           cout << m1 << endl;                                                              unordered_map<string,int>::mapped_type m2 = imap[2];//m2为int                    cout << m2 << endl;                                                              smap["aa"] = 33;                                                                 cout << smap["aa"] << endl;                                                      smap["cc"] = 13;//想smap添加{"cc",13}                                            cout << smap["cc"] << endl;                                                      cout << smap.at("cc") << endl;                                                   //cout << smap.at("ccd") << endl;//抛出out_of_range异常                          map<string,int>::mapped_type m3 = smap.at("aa");                                 cout << m3 << endl;                                                              //想smap里添加了{"dd", 0},                                                       cout << smap["dd"] << endl;                                                      for(auto const &s : smap){                                                         cout << s.first << "," << s.second << endl;                                    }                                                                                */    //test2 map 用自定义类型的下标操作                                               /*                                                                               map<test,int> tmap{{test(10), 10},{test(11), 11}};                               tmap[test()] = 1;                                                                for(auto const &s : tmap){                                                         cout << s.first.getdata() << "," << s.second << endl;                          }                                                                                */    //test3 map的查找                                                                /*                                                                               map<int, int> imap{{1,1},{3,3},{2,2},{5,5},{4,4}};                               map<int,int>::iterator it1 = imap.find(1);                                       cout << it1->first << endl;                                                      map<int,int>::iterator it2 = imap.find(4);//返回imap.end()                      if(it2 == imap.end()){cout << "it2 is end" << endl;}                             cout << imap.count(2) << endl;                                                   auto it3 = imap.lower_bound(2);//{2,2}                                           cout << it3->first << "," << it3->second << endl;                                auto it4 = imap.upper_bound(4);//{5,5}                                           cout << it4->first << "," << it4->second << endl;                                */    //test4 multimap的查找                                                           multimap<string, string> autrs{{"aaa","n1"},{"bbb","n1"},{"aaa","n2"},                                                              {"aaa","n3"}};   string sch("aaa");   //方法1                                                                          auto cnt = autrs.count(sch);   auto it = autrs.find(sch);   while(cnt){     cout << it->second << endl;     ++it;     --cnt;   }   cout << "-----------------" << endl;   //方法2                                                                          for(auto beg = autrs.lower_bound(sch),end = autrs.upper_bound(sch);       beg != end; ++beg){     cout << beg->second << endl;   }   cout << "-----------------" << endl;   //方法3                                                                          for(auto pos = autrs.equal_range(sch);pos.first != pos.second;++pos.first){     cout << pos.first->second << endl;   } }

c/c++ 学习互助qq群:877684253

c/c++ 标准库 map multimap元素访问

本人微信:xiaoshitou5854

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/607614.html

(0)
上一篇 2021年5月14日
下一篇 2021年5月14日

精彩推荐