map::equal_range
描述 (Description)
C ++函数std::map::equal_range()返回与特定键匹配的元素范围。
范围由两个迭代器定义,一个指向第一个元素,不小于键k ,另一个指向第一个元素大于键k 。
声明 (Declaration)
以下是std :: map :: equal_range()函数形式std :: map头的声明。
C++98
pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
pair<iterator,iterator> equal_range (const key_type& k);
参数 (Parameters)
k - 要搜索的密钥。
返回值
如果object是常量限定的,则method返回一对常量迭代器,否则返回一对非常量迭代器。
异常 (Exceptions)
该成员函数不会抛出异常。
时间复杂
对数即O(log n)
例子 (Example)
以下示例显示了std :: map :: equal_range()函数的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
map<char, int> m = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
};
auto ret = m.equal_range('b');
cout << "Lower bound is " << ret.first->first <<
" = " << ret.first->second << endl;
cout << "Upper bound is " << ret.second->first <<
" = " << ret.second->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Lower bound is b = 2
Upper bound is c = 3