unordered_multimap::find()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向具有键k的元素之一。如果容器不包含键为k的任何元素,则它将返回一个迭代器,该迭代器指向经过容器中最后一个元素的位置。
用法:
unordered_multimap_name.find(k)
参数:该函数接受指定 key 的强制参数k。
返回值:它返回一个迭代器,该迭代器指向具有键k的元素所在的位置。
以下示例程序旨在说明上述函数:
示例1:
// C++ program to illustrate the
// unordered_multimap::find() function
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap<int, int> sample;
// inserts key and element
sample.insert({ 1, 2 });
sample.insert({ 1, 2 });
sample.insert({ 2, 3 });
sample.insert({ 3, 4 });
sample.insert({ 2, 6 });
// find the element with key 1 and print
auto it = sample.find(1);
if (it != sample.end())
cout << 1 << ":" << it->second << endl;
else
cout << "element with key 1 not found\n";
// find the element with
// key 2 and print
it = sample.find(2);
if (it != sample.end())
cout << 2 << ":" << it->second << endl;
else
cout << "element with key 2 not found\n";
// find the element with
// key 100 and print
it = sample.find(100);
if (it != sample.end())
cout << 100 << ":" << it->second << endl;
else
cout << "element with key 100 not found\n";
return 0;
}
输出:
1:2 2:6 element with key 100 not found
示例2:
// C++ program to illustrate the
// unordered_multimap::find()
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap<char, char> sample;
// inserts element
sample.insert({ 'a', 'b' });
sample.insert({ 'a', 'b' });
sample.insert({ 'a', 'd' });
sample.insert({ 'b', 'e' });
sample.insert({ 'b', 'd' });
// find the element with
// key r and print
auto it = sample.find('r');
if (it != sample.end())
cout << "r"
<< ":" << it->second << endl;
else
cout << "element with key r not found\n";
// find the element with
// key a and print
it = sample.find('a');
if (it != sample.end())
cout << 'a' << ":" << it->second << endl;
else
cout << "element with key a not found\n";
// find the element with
// key 'b' and print
it = sample.find('b');
if (it != sample.end())
cout << "b"
<< ":" << it->second << endl;
else
cout << "element with key b not found\n";
return 0;
}
输出:
element with key r not found a:d b:d
相关用法
- C++ map find()用法及代码示例
- C++ set find()用法及代码示例
- C++ multiset find()用法及代码示例
- C++ unordered_set find()用法及代码示例
- C++ unordered_multiset find()用法及代码示例
- C++ std::find用法及代码示例
- C++ unordered_map find用法及代码示例
- C++ multimap find()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 unordered_multimap find() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。