C++ STL中的无序序集

等_范围() 一般来说,返回的范围包括与给定值相等的所有元素。对于 无序集 如果所有键都不同,则返回的范围最多包含一个元素。

null

语法

 setname.equal_range(key name)

论据 它将要搜索的密钥作为参数。 返回值 它返回两个迭代器——包含键的范围的下限和上限。 实例

// C++ program to illustrate the
// unordered_set::equal_range function
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
// declaration
unordered_set< int > sample;
// Insert some values
sample.insert({ 20, 30, 40 });
// Test the equal_range function for
// a given key if it does exists
auto range1 = sample.equal_range(20);
if (range1.first != sample.end()) {
for (; range1.first != range1.second; ++range1.first)
cout << *range1.first << endl;
}
else
cout << "Element does not exist" ;
return 0;
}


输出

20

// C++ program to illustrate the
// unordered_set::equal_range function
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
// declaration
unordered_set< int > sample;
// Insert some values
sample.insert({ 20, 30, 40 });
// Test the equal_range function
// for a given key if it does not exist
auto range1 = sample.equal_range(60);
if (range1.first != sample.end()) {
for (; range1.first != range1.second; ++range1.first)
cout << *range1.first << endl;
}
else
cout << "Element does not exist" ;
return 0;
}


输出

Element does not exist
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享