unordered_multimap :: find()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向具有键k的元素之一。如果容器不包含键为k的任何元素,则它将返回一个迭代器,该迭代器指向该容器中最后一个元素之后的位置。
句法:
unordered_multimap_name.find(k)
参数:该函数接受一个必填参数k ,该参数指定了密钥。
返回值:它返回一个迭代器,该迭代器指向具有键k的元素所在的位置。
下面的程序说明了上述函数:
程序1:
// C++ program to illustrate the
// unordered_multimap::find() function
#include
#include
using namespace std;
int main()
{
// declaration
unordered_multimap 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
#include
using namespace std;
int main()
{
// declaration
unordered_multimap 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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。