本篇学习unordered_map的查找操作和观察器相关的操作,具体的函数如下:
(C++11) | 返回匹配特定键的元素数量 (公开成员函数) |
(C++11) | 寻找带有特定键的元素 (公开成员函数) |
(C++20) | 检查容器是否含有带特定键的元素 (公开成员函数) |
(C++11) | 返回匹配特定键的元素范围 (公开成员函数) |
(C++11) | 返回用于对键散列的函数 (公开成员函数) |
(C++11) | 返回用于比较键的相等性的函数 (公开成员函数) |
示例代码:
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
void findOpertion()
{
//1.count返回匹配特定键的元素数量
std::unordered_map<int, std::string> map1;
map1.emplace(1, "Hero");
map1.emplace(2, "Archer");
map1.emplace(3, "Barry");
int nkey = 0;
for(nkey = 1; nkey < 6; ++nkey)
{
if(map1.count(nkey) > 0)
std::cout << nkey << " is element of map1\n";
else
std::cout << nkey << " is not element of map1\n";
}
//2.find寻找带有特定键的元素,返回的是迭代器
std::unordered_map<int, std::string> map2;
map2.emplace(4, "camel");
map2.emplace(5, "iran");
std::unordered_map<int, std::string>::iterator it;
it = map2.find(4);
if(it != map2.end())
std::cout << "find element is " << it->second << std::endl;
else
std::cout << "not find element\n";
//3.equal_range返回匹配特定键的元素范围,返回容器中所有拥有给定关键的元素范围。范围以二个迭代器定义,一个指向首个不小于 key 的元素,
//另一个指向首个大于 key 的元素。首个迭代器可以换用 lower_bound() 获得,而第二迭代器可换用 upper_bound() 获得。
std::unordered_map<int, std::string> map3;
map3.emplace(1, "scott");
map3.emplace(3, "camel");
map3.emplace(5, "Sky");
map3.emplace(7, "beer");
pair<unordered_map<int, string>::iterator, unordered_map<int, string>::iterator> it2;
pair<unordered_map<int, string>::iterator, unordered_map<int, string>::iterator> it3;
std::unordered_map<int,char> map = {{1,'a'},{1,'b'},{3,'d'},{5,'b'}};
auto range = map.equal_range(1);
for (auto it = range.first; it != range.second; ++it) {
std::cout << it->first << ' ' << it->second << '\n';
}
std::cout << range.first->first << " => " << range.first->second << std::endl;
std::cout << range.second->first << " => " << range.second->second << std::endl;
//print
it2 = map3.equal_range(1);//返回的第一个迭代器为传进来的关键字的值,
//这个与map里返回的值有差别,如果没有这个关键字,则返回end()迭代器。
//在map里equal_range(2)返回3的迭代器,而在这里返回的是end()迭代器
if(it2.first != map3.end())
{
std::cout << "lower bound points to: ";//返回的第一个迭代器为传进来的关键字的值
std::cout << it2.first->first << " => " << it2.first->second << std::endl;
}
else
{
std::cout << "not find lower range" << endl;
}
if(it2.second != map3.end())
{
std::cout << "upper bound points to: ";//返回的第二个迭代器首个大于传进来的关键字的值
std::cout << it2.second->first << " => " << it2.second->second << std::endl;
}
else
{
std::cout << "not find upper range" << endl;
}
std::cout << "======================================\n";
it3 = map3.equal_range(3);
std::cout << "lower bound points to: ";
std::cout << it3.first->first << " => " << it3.first->second << std::endl;
std::cout << "upper bound points to: ";
std::cout << it3.second->first << " => " << it3.second->second << std::endl;
//4.lower_bound返回指向首个等于 key 的元素的迭代器。若找不到这种元素,则返回尾后迭代器
std::unordered_map<int, std::string>::iterator lowerIt;
//lowerIt = map3.lower_bound(2);//这个key一定要存在unordered_map中,否则返回end()迭代器
//std::cout << "lowerIt->first = " << lowerIt->first << " lowerIt->second = " << lowerIt->second << std::endl;
lowerIt = map3.lower_bound(3);
std::cout << "lowerIt->first = " << lowerIt->first << " lowerIt->second = " << lowerIt->second << std::endl;
//5.upper_bound返回指向首个大于 key 的元素的迭代器。若找不到这种元素,则返回尾后。
std::unordered_map<int, std::string>::iterator upperIt;
//upperIt = map3.upper_bound(4);//这个key一定要存在unordered_map中,否则返回end()迭代器
//std::cout << "upperIt->first = " << upperIt->first << " upperIt->second = " << upperIt->second << std::endl;
upperIt = map3.upper_bound(5);
std::cout << "upperIt->first = " << upperIt->first << " upperIt->second = " << upperIt->second << std::endl;
//6. key_comp 返回用于比较键的函数
std::unordered_map<int, std::string> map4;
map4.emplace(1, "scott");
map4.emplace(3, "beer");
map4.emplace(5, "Sky");
map4.emplace(7, "camel");
std::unordered_map<int, std::string>::key_compare funCompare = map4.key_comp();//返回一个比较键的函数
bool b1 = funCompare(3, 5);
bool b2 = funCompare(5, 3);
std::cout << "b1 = " << b1 << " b2 = " << b2 << std::endl;
//7. value_comp返回用于在value_type类型的对象中比较键的函数。
std::unordered_map<char, int> map5;
map5.emplace('1', 12);
map5.emplace('3', 8);
map5.emplace('5', 54);
map5.emplace('7', 6);
std::unordered_map<char, int>::iterator endIt = map5.end();
auto lastElement = --endIt;//最后一个元素
std::cout << "lastElement.key = " << lastElement->first << " lastElement.value = " << lastElement->second << std::endl;
std::unordered_map<char, int>::iterator firstIt = map5.begin();//第一个元素迭代器
bool b3 = map5.value_comp()(*firstIt, *lastElement);
++firstIt;
bool b4 = map5.value_comp()(*firstIt, *lastElement);
std::cout << "b3 = " << b3 << " b4 = " << b4 << std::endl;
//8. hash_function 返回对关键哈希的函数
unordered_map<int, string> map6;
unordered_map<int, string>::hasher fn = map6.hash_function();
std::cout << "fn1 = " << fn(1) << " fn2 = " << fn(2) << std::endl;
//9. key_eq 返回用于比较键的相等性的函数
unordered_map<string, string> map7;
bool case_insensitive = map7.key_eq()("camle","CAMLE");
std::cout << "map7.key_eq() is ";
std::cout << ( case_insensitive ? "case insensitive" : "case sensitive" );
std::cout << std::endl;
//case sensitive:区分大小写
//case insensitive不区分大小写
}
int main()
{
findOpertion();
cout << "hello world" << endl;
return 0;
}
运行结果:
参考:
https://zh.cppreference.com/w/cpp/container/unordered_map
http://www.cplusplus.com/reference/unordered_map/unordered_map/