C++ 算法 equal_range() 函數是二分查找的版本。該函數用於返回子範圍的下界和上界,該子範圍包括範圍[first, last)中所有等價於val的元素。
其中子範圍由兩個迭代器定義,一個指向不小於 val 的第一個元素,另一個指向大於 val 的第一個元素。
- 第一個版本使用運算符 < 來比較元素,第二個版本使用給定的比較函數,即 comp。
- 範圍 [first, last) 必須根據與 val 的比較進行分區,即它必須滿足以下所有條件:
- 相對於 element < val 或 comp(element, val) 進行分區
- 相對於 !(val < element) 或 !comp(val, element) 進行分區
- 對於所有元素,如果 element < val 或 comp(element, val) 為真,則 !(val < element) 或 !comp(val, element) 也為真。
用法
default (1) template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range (ForwardIterator first, ForwardIterator last, const T& val);
custom (2) template <class ForwardIterator, class T, class Compare>
pair<ForwardIterator,ForwardIterator>
equal_range (ForwardIterator first, ForwardIterator last, const T& val,
Compare comp);
參數
first:指向要搜索的範圍中的第一個元素的前向迭代器。
last:指向要搜索範圍中過去最後一個元素的前向迭代器。
補償: 一個用戶定義的二元謂詞函數,它接受兩個參數,如果兩個參數按順序返回真,否則返回假。它遵循嚴格的弱排序來對元素進行排序。
val: 用於比較範圍內元素的上限值。
返回值
它返回兩個迭代器,一個指向第一個不小於 val 的元素,另一個指向第一個大於 val 的元素。
如果沒有找到這樣的元素,那麽它最後返回。
複雜度
平均而言,複雜性在 first 和 last 之間的距離上是對數的:最多執行 2*log2 (N) + 1 次元素比較,其中 N = last - first。
數據競爭
訪問範圍 [first, last) 中的對象。
異常
如果元素比較或迭代器上的操作引發異常,則此函數將引發異常。
請注意,無效參數會導致未定義的行為。
例子1
讓我們看一個簡單的例子來演示 equal_range() 的使用:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = {3, 1, 4, 2, 5};
sort(v.begin(), v.end());
auto result = equal_range(v.begin(), v.end(), 3);
cout << "Lower Bound of 3 is:"<<*result.first << endl;
cout << "Upper Bound of 3 is:"<<*result.second << endl;
return 0;
}
輸出:
Lower Bound of 3 is:3 Upper Bound of 3 is:4
例子2
讓我們看另一個使用 operator< 比較元素的簡單示例:
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
struct S
{
int number;
char name;
S ( int number, char name )
:number ( number ), name ( name )
{}
// only the number is relevant with this comparison
bool operator< ( const S& s ) const
{
return number < s.number;
}
};
int main()
{
// note:not ordered, only partitioned w.r.t. S defined below
vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4,'G'}, {3,'F'} };
S value ( 2, '?' );
auto p = equal_range(vec.begin(),vec.end(),value);
for ( auto i = p.first; i != p.second; ++i )
cout << i->name << ' ';
return 0;
}
輸出:
B C D
在上麵的例子中,運算符 < 用於比較元素並返回等於 2 的範圍內的所有元素。
例子3
讓我們看另一個使用比較函數比較元素的簡單示例:
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
struct S
{
int number;
char name;
S ( int number, char name )
:number ( number ), name ( name )
{}
// only the number is relevant with this comparison
bool operator< ( const S& s ) const
{
return number < s.number;
}
};
struct Comp
{
bool operator() ( const S& s, int i )
{
return s.number < i;
}
bool operator() ( int i, const S& s )
{
return i < s.number;
}
};
int main()
{
// note:not ordered, only partitioned w.r.t. S defined below
vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4,'G'}, {3,'F'} };
auto p = equal_range(vec.begin(),vec.end(),2,Comp());
for ( auto i = p.first; i != p.second; ++i )
cout << i->name << ' ';
return 0;
}
輸出:
B C D
示例 4
讓我們看另一個簡單的例子:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {2, 3, 5, 6, 7, 7, 7, 8, 9, 10};
vector<int> v(a, a+10);
cout <<"\nHere are the contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
pair<vector<int>::iterator, vector<int>::iterator> bounds;
bounds = equal_range(v.begin(), v.end(), 3);
if (bounds.first != v.end())
cout <<"\nLower bound of 3 in v = "<<*bounds.first;
if (bounds.first != v.end())
cout <<"\nUpper bound of 3 in v = "<<*bounds.second;
bounds = equal_range(v.begin(), v.end(), 4);
if (bounds.first != v.end())
cout <<"\nLower bound of 4 in v = "<<*bounds.first;
if (bounds.first != v.end())
cout <<"\nUpper bound of 4 in v = "<<*bounds.second;
bounds = equal_range(v.begin(), v.end(), 5);
if (bounds.first != v.end())
cout <<"\nLower bound of 5 in v = "<<*bounds.first;
if (bounds.first != v.end())
cout <<"\nUpper bound of 5 in v = "<<*bounds.second;
bounds = equal_range(v.begin(), v.end(), 7);
if (bounds.first != v.end())
cout <<"\nLower bound of 7 in v = "<<*bounds.first;
cout <<"\nThis is the first of the three 7's, since the value "
"before this 7 is "<<*(bounds.first-1)<<".";
if (bounds.first != v.end())
cout <<"\nUpper bound of 7 in v = "<<*bounds.second;
bounds = equal_range(v.begin(), v.end(), 0);
if (bounds.first != v.end())
cout <<"\nLower bound of 0 in v = "<<*bounds.first;
if (bounds.first != v.end())
cout <<"\nUpper bound of 0 in v = "<<*bounds.second;
bounds = equal_range(v.begin(), v.end(), 15);
if (bounds.first != v.end())
cout <<"\nLower bound of 15 in v = "<<*bounds.first;
if (bounds.first != v.end())
cout <<"\nUpper bound of 15 in v = "<<*bounds.second;
cout <<"\nNote that both the lower and upper bound locations "
"\nof 15 are the end (one-past-the-last) vector position.";
return 0;
}
輸出:
Here are the contents of v: 2 3 5 6 7 7 7 8 9 10 Lower bound of 3 in v = 3 Upper bound of 3 in v = 5 Lower bound of 4 in v = 5 Upper bound of 4 in v = 5 Lower bound of 5 in v = 5 Upper bound of 5 in v = 6 Lower bound of 7 in v = 7 This is the first of the three 7's, since the value before this 7 is 6. Upper bound of 7 in v = 8 Lower bound of 0 in v = 2 Upper bound of 0 in v = 2 Note that both the lower and upper bound locations of 15 are the end (one-past-the-last) vector position.
相關用法
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
- C++ Algorithm transform()用法及代碼示例
- C++ Algorithm set_difference()用法及代碼示例
- C++ Algorithm is_sorted()用法及代碼示例
- C++ Algorithm minmax_element()用法及代碼示例
- C++ Algorithm stable_sort()用法及代碼示例
- C++ Algorithm min()用法及代碼示例
- C++ Algorithm stable_partition()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm equal_range()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。