无序的多重映射::相等的范围() 在C++ STL中是一个内置函数,它返回所有元素的键等于A的范围。 钥匙 。它返回一对迭代器,其中第一个是指向范围下限的迭代器,第二个是指向范围上限的迭代器。如果没有一个元素等于给定的 价值 在容器中,然后返回一对,其中下限和上限都指向超过容器或容器末端的位置 无序的多重地图。结束()。
null
语法:
unordered_multimap_name.equal_range(k)
参数: 该函数接受一个强制参数 K .返回的范围将包含键为k的元素。
返回值: 它返回一对迭代器。
以下程序说明了上述功能:
项目1:
// C++ program to illustrate the // unordered_multimap::equal_range() #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 }); // iterator of pairs pointing to range // which includes 1 and print by iterating in range auto itr = sample.equal_range(1); cout << "Elements with Key 1: " ; for ( auto it = itr.first; it != itr.second; it++) { cout << it->second << " " ; } cout << endl; // iterator of pairs pointing to range // which includes 2 and print by iterating in range itr = sample.equal_range(2); cout << "Elements with Key 2: " ; for ( auto it = itr.first; it != itr.second; it++) { cout << it->second << " " ; } return 0; } |
输出:
Elements with Key 1: 2 2 Elements with Key 2: 6 3
项目2:
// C++ program to illustrate the // unordered_multimap::equal_range() #include <iostream> #include <unordered_map> using namespace std; int main() { // declaration unordered_multimap< char , char > sample; // inserts key and element sample.insert({ 'a' , 'b' }); sample.insert({ 'a' , 'b' }); sample.insert({ 'a' , 'd' }); sample.insert({ 'b' , 'e' }); sample.insert({ 'b' , 'd' }); // iterator of pairs pointing to range // which includes b and print by iterating in range auto itr = sample.equal_range( 'b' ); cout << "Elements with Key b: " ; for ( auto it = itr.first; it != itr.second; it++) { cout << it->second << " " ; } cout << endl; // iterator of pairs pointing to range // which includes a and print by iterating in range itr = sample.equal_range( 'a' ); cout << "Elements with Key a: " ; for ( auto it = itr.first; it != itr.second; it++) { cout << it->second << " " ; } return 0; } |
输出:
Elements with Key b: d e Elements with Key a: d b b
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END