返回匹配特定键的元素数量 (公开成员函数) | |
寻找带有特定键的元素 (公开成员函数) | |
(C++20) | 检查容器是否含有带特定键的元素 (公开成员函数) |
返回匹配特定键的元素范围 (公开成员函数) | |
返回指向首个不小于给定键的元素的迭代器 (公开成员函数) | |
返回指向首个大于给定键的元素的迭代器 (公开成员函数) | |
观察器 | |
返回用于比较键的函数 (公开成员函数) | |
返回用于在value_type类型的对象中比较键的函数。 (公开成员函数) |
示例代码
#include <iostream>
#include <map>
#include <string>
void findOpertion()
{
//1.count返回匹配特定键的元素数量
std::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::map<int, std::string> map2;
map2.emplace(4, "camel");
map2.emplace(5, "iran");
std::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::map<int, std::string> map3;
map3.emplace(1, "scott");
map3.emplace(3, "camel");
map3.emplace(5, "Sky");
map3.emplace(7, "beer");
std::pair<std::map<int, std::string>::iterator, std::map<int, std::string>::iterator> it2;
std::pair<std::map<int, std::string>::iterator, std::map<int, std::string>::iterator> it3;
//print
it2 = map3.equal_range(2);
std::cout << "lower bound points to: ";//返回的第一个迭代器不小于传进来的关键字的值
std::cout << it2.first->first << " => " << it2.first->second << std::endl;
std::cout << "upper bound points to: ";//返回的第二个迭代器首个大于传进来的关键字的值
std::cout << it2.second->first << " => " << it2.second->second << std::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::map<int, std::string>::iterator lowerIt;
lowerIt = map3.lower_bound(2);
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::map<int, std::string>::iterator upperIt;
upperIt = map3.upper_bound(4);
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::map<int, std::string> map4;
map4.emplace(1, "scott");
map4.emplace(3, "beer");
map4.emplace(5, "Sky");
map4.emplace(7, "camel");
std::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::map<char, int> map5;
map5.emplace('1', 12);
map5.emplace('3', 8);
map5.emplace('5', 54);
map5.emplace('7', 6);
std::pair<char, int> lastElement = *map5.rbegin();//最后一个元素
std::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;
}
int main()
{
findOpertion();
cout << "Hello World!" << endl;
return 0;
}
运行结果 :
参考:
http://www.cplusplus.com/reference/map/map/
https://zh.cppreference.com/w/cpp/container/map