热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

unordered_multimap学习之查找操作和观察器操作count,find,contains(C++20),equal_range,hash_function,key_eq

本篇学习unordered_multimap的查找操作和观察器操作,具体函数如下:count(C11)返回匹配特定键的元素数量(公开成员函数)find(

本篇学习unordered_multimap的查找操作和观察器操作,具体函数如下:

count

(C++11)

返回匹配特定键的元素数量
(公开成员函数)

find

(C++11)

寻找带有特定键的元素
(公开成员函数)

contains

(C++20)

检查容器是否含有带特定键的元素
(公开成员函数)

equal_range

(C++11)

返回匹配特定键的元素范围
(公开成员函数)

hash_function

(C++11)

返回用于对键散列的函数
(公开成员函数)

key_eq

(C++11)

返回用于比较键的相等性的函数
(公开成员函数)

代码示例:

#include
#include
#include using namespace std;void findOpertion()
{//1.count返回匹配特定键的元素数量std::unordered_multimap map1;map1.emplace(1, "Hero");map1.emplace(2, "Archer");map1.emplace(3, "Barry");int nkey &#61; 0;for(nkey &#61; 1; nkey <6; &#43;&#43;nkey){if(map1.count(nkey) > 0)std::cout < map2;map2.emplace(4, "camel");map2.emplace(5, "iran");std::unordered_multimap::iterator it;it &#61; map2.find(4);if(it !&#61; map2.end())std::cout <<"find element is " <second < map3;map3.emplace(1, "scott");map3.emplace(3, "camel");map3.emplace(5, "Sky");map3.emplace(7, "beer");pair::iterator, unordered_multimap::iterator> it2;pair::iterator, unordered_multimap::iterator> it3;std::unordered_multimap map &#61; {{1,&#39;a&#39;},{1,&#39;b&#39;},{3,&#39;d&#39;},{5,&#39;b&#39;}};auto range &#61; map.equal_range(1);for (auto it &#61; range.first; it !&#61; range.second; &#43;&#43;it) {std::cout <first <<&#39; &#39; <second <<&#39;\n&#39;;}std::cout <first <<" &#61;> " <second <first <<" &#61;> " <second <first <<" &#61;> " <second <first <<" &#61;> " <second <first <<" &#61;> " <second <first <<" &#61;> " <second <::iterator lowerIt;//lowerIt &#61; map3.lower_bound(2);//这个key一定要存在unordered_map中&#xff0c;否则返回end()迭代器//std::cout <<"lowerIt->first &#61; " <first <<" lowerIt->second &#61; " <second <first &#61; " <first <<" lowerIt->second &#61; " <second <::iterator upperIt;//upperIt &#61; map3.upper_bound(4);//这个key一定要存在unordered_map中&#xff0c;否则返回end()迭代器//std::cout <<"upperIt->first &#61; " <first <<" upperIt->second &#61; " <second <first &#61; " <first <<" upperIt->second &#61; " <second < map4;map4.emplace(1, "scott");map4.emplace(3, "beer");map4.emplace(5, "Sky");map4.emplace(7, "camel");std::unordered_multimap::key_compare funCompare &#61; map4.key_comp();//返回一个比较键的函数bool b1 &#61; funCompare(3, 5);bool b2 &#61; funCompare(5, 3);std::cout <<"b1 &#61; " < map5;map5.emplace(&#39;1&#39;, 12);map5.emplace(&#39;3&#39;, 8);map5.emplace(&#39;5&#39;, 54);map5.emplace(&#39;7&#39;, 6);std::unordered_multimap::iterator endIt &#61; map5.end();auto lastElement &#61; --endIt;//最后一个元素std::cout <<"lastElement.key &#61; " <first <<" lastElement.value &#61; " <second <::iterator firstIt &#61; map5.begin();//第一个元素迭代器bool b3 &#61; map5.value_comp()(*firstIt, *lastElement);&#43;&#43;firstIt;bool b4 &#61; map5.value_comp()(*firstIt, *lastElement);std::cout <<"b3 &#61; " < map6;unordered_multimap::hasher fn &#61; map6.hash_function();std::cout <<"fn1 &#61; " < map7;bool case_insensitive &#61; map7.key_eq()("camle","CAMLE");std::cout <<"map7.key_eq() is ";std::cout <<( case_insensitive ? "case insensitive" : "case sensitive" );std::cout <}int main()
{findOpertion();cout <<"hello world" <}

运行结果&#xff1a;

 

参考&#xff1a;

https://zh.cppreference.com/w/cpp/container/unordered_multimap


推荐阅读
author-avatar
海马33电影网-2
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有