标准:等_范围 用于查找给定范围[first,last]内的子范围,该范围内的所有元素都等效于给定值。它 返回此类子范围的初始和最终界限。
null
此函数要求根据某些条件对范围进行排序或分区,以便条件计算为true的所有元素都位于给定值的左侧,其余元素都位于其右侧。
它有两种使用方式,如下所示:
- 使用 <:>
语法:
Template pair equal_range (ForwardIterator first, ForwardIterator last, const T& val); first: Forward iterator to the first element in the range. last: Forward iterator to the last element in the range. val: Value of the subrange to search for in the range. Return Value: It returns a pair object, whose member pair::first is an iterator to the lower bound of the subrange of equivalent values, and pair::second its upper bound. If there is no element equivalent to val, then both first and second points to the nearest element greater than val, or if val is greater than any other value, then both of them point to last.
// C++ program to demonstrate the use of std::equal_range
#include <iostream>
#include <vector>
#include <algorithm>
using
namespace
std;
int
main()
{
vector<
int
> v = { 10, 10, 30, 30, 30, 100, 10,
300, 300, 70, 70, 80 };
// Declaring an iterator to store the
// return value of std::equal_range
std::pair<std::vector<
int
>::iterator,
std::vector<
int
>::iterator> ip;
// Sorting the vector v
sort(v.begin(), v.end());
// v becomes 10 10 10 30 30 30 70 70 80 100 300 300
// Using std::equal_range and comparing the elements
// with 30
ip = std::equal_range(v.begin(), v.begin() + 12, 30);
// Displaying the subrange bounds
cout <<
"30 is present in the sorted vector from index "
<< (ip.first - v.begin()) <<
" till "
<< (ip.second - v.begin());
return
0;
}
输出:
30 is present in the sorted vector from index 3 till 6
说明: 在对向量v1进行排序后,我们检查了30存在的范围,即从索引3到索引6。
- 通过使用预定义函数进行比较:
语法:
pair equal_range (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); Here, first, last and val are the same as previous case. comp: Binary function that accepts two arguments of the type pointed by ForwardIterator (and of type T), and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second. The function shall not modify any of its arguments. This can either be a function pointer or a function object. Return Value: It returns a pair object, whose member pair::first is an iterator to the lower bound of the subrange of equivalent values, and pair::second its upper bound. If there is no element equivalent to val, then both first and second point to the nearest element greater than val, or if val is greater than any other value, then both of them point to last.
// C++ program to demonstrate the use of std::equal_range
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <functional>
using
namespace
std;
// Defining the BinaryFunction
bool
comp(
int
a,
int
b)
{
return
(a > b);
}
int
main()
{
vector<
int
> v = { 10, 10, 30, 30, 30, 100, 10,
300, 300, 70, 70, 80 };
// Declaring an iterator to store the
// return value of std::equal_range
std::pair<std::vector<
int
>::iterator,
std::vector<
int
>::iterator> ip;
// Sorting the vector v in descending order
sort(v.begin(), v.end(), greater<
int
>());
// v becomes 300 300 100 80 70 70 30 30 30 10 10 10
// Using std::equal_range and comparing the elements
// with 10
ip = std::equal_range(v.begin(), v.begin() + 12, 10, comp);
// Displaying the subrange bounds
cout <<
"10 is present in the sorted vector from index "
<< (ip.first - v.begin()) <<
" till "
<< (ip.second - v.begin());
return
0;
}
输出:
10 is present in the sorted vector from index 9 till 12
在哪里可以使用?
- 下界 和 上界 在一个地方: 如果我们想同时使用std::lower_bound和std::upper_bound,可以使用这个函数,因为它的第一个指针将与std::lower_bound相同,第二个指针将与std::upper_bound相同。所以,如果我们有std::equal_range,那么单独使用它们是没有用的。
// C++ program to demonstrate the use of std::equal_range
#include <iostream>
#include <vector>
#include <algorithm>
using
namespace
std;
int
main()
{
vector<
int
> v = { 1, 2, 3, 4, 5, 5, 6, 7 };
// Declaring an iterator to store the
// return value of std::equal_range
std::pair<std::vector<
int
>::iterator,
std::vector<
int
>::iterator> ip;
// Using std::equal_range and comparing the elements
// with 5
ip = std::equal_range(v.begin(), v.end(), 5);
// Displaying the subrange bounds
cout <<
"std::lower_bound should be equal to "
<< (ip.first - v.begin()) <<
" and std::upper_bound "
<<
"should be equal to "
<< (ip.second - v.begin());
vector<
int
>::iterator i1, i2;
// Using std::lower_bound
i1 = std::lower_bound(v.begin(), v.end(), 5);
cout <<
"std::lower_bound is = "
<< (i1 - v.begin());
// Using std::upper_bound
i2 = std::upper_bound(v.begin(), v.end(), 5);
cout <<
"std::upper_bound is = "
<< (i2 - v.begin());
return
0;
}
输出:
std::lower_bound should be equal to 4 and std::upper_bound should be equal to 6 std::lower_bound is = 4 std::upper_bound is = 6
本文由 辛格先生 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END