定义于头文件
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
std::lower_bound
template< class ForwardIt, class T > | (1) | (C++20 前) |
template< class ForwardIt, class T > | (C++20 起) | |
template< class ForwardIt, class T, class Compare > | (2) | (C++20 前) |
template< class ForwardIt, class T, class Compare > | (C++20 起) |
返回指向范围 [first, last)
中首个不小于(即大于或等于) value
的元素的迭代器,或若找不到这种元素则返回 last
。
范围 [first, last)
必须已相对于表达式 element < value 或 comp(element, value) 划分,即所有令该表达式为 true 的元素必须前趋所有令该表达式为 false 的元素。完全排序的范围满足此判别标准。
第一版本用 operator< 比较元素,第二版本用给定的比较函数 comp
。
first, last | - | 定义要检验的已部分排序范围的迭代器 |
value | - | 要与元素比较的值 |
comp | - | 若第一参数小于(即先序于)第二个则返回 true 的二元谓词。 谓词函数的签名应等价于如下: bool pred(const Type1 &a, const Type2 &b); 虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
类型要求 | ||
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。 | ||
- Compare 必须满足二元谓词 (BinaryPredicate) 的要求。不要求满足 比较 (Compare) 。 |
指向首个不小于 value
的元素的迭代器,或若找不到这种元素则为 last
。
进行的比较次数与 first
和 last
间的距离成对数(至多 log
2(last - first) + O(1) 次比较)。然而,对于非遗留随机访问迭代器 (LegacyRandomAccessIterator) ,迭代次自增次数为线性。
template
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value)
{ForwardIt it;typename std::iterator_traits::difference_type count, step;count = std::distance(first, last);while (count > 0) {it = first; step = count / 2; std::advance(it, step);if (*it < value) {first = ++it; count -= step + 1; }elsecount = step;}return first;
}
template
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{ForwardIt it;typename std::iterator_traits::difference_type count, step;count = std::distance(first, last);while (count > 0) {it = first;step = count / 2;std::advance(it, step);if (comp(*it, value)) {first = ++it;count -= step + 1;}elsecount = step;}return first;
}
#include
#include
#include
#include
#include
#include using namespace std;struct Cell
{int x;int y;Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}template
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp = {})
{// 注意:类型 T 和 Forward 解引用后的类型都必须可隐式转换为// 用于 Compare 的 Type1 和 Type2 。// 这严格于 lower_bound 要求(见上述)first = std::lower_bound(first, last, value, comp);return first != last && !comp(value, *first) ? first : last;
}int main()
{srand((unsigned)time(NULL));;std::cout.setf(std::ios_base::boolalpha);auto func1 = [](){int n = std::rand() % 10 + 100;Cell cell{n, n};return cell;};vector cells(8);std::generate(cells.begin(), cells.end(), func1);std::sort(cells.begin(), cells.end());std::cout << "cells : ";std::copy(cells.begin(), cells.end(), std::ostream_iterator(std::cout, " "));std::cout << std::endl << std::endl;auto lower = std::lower_bound(cells.begin(), cells.end(), *(cells.begin() + 3));auto upper = std::upper_bound(cells.begin(), cells.end(), *(cells.begin() + 3));std::cout << "cells lower upper: ";std::copy(lower, upper, std::ostream_iterator(std::cout, " "));std::cout << std::endl << std::endl;auto is_sortfa = [](const Cell & a, const Cell & b){if (a.x == b.x){return a.y < b.y;}return a.x < b.x;};auto is_sortfb = [](const Cell & a, const Cell & b){if (a.x == b.x){return a.y > b.y;}return a.x > b.x;};lower = std::lower_bound(cells.begin(), cells.end(), *(cells.begin() + 3), is_sortfa);upper = std::upper_bound(cells.begin(), cells.end(), *(cells.begin() + 3), is_sortfb);std::cout << "cells lower upper: ";std::copy(lower, upper, std::ostream_iterator(std::cout, " "));std::cout << std::endl << std::endl;auto it = binary_find(cells.cbegin(), cells.cend(), *(cells.begin() + 3), is_sortfa);if (it != cells.cend()){std::cout << *it << " found at index " << std::distance(cells.cbegin(), it);}std::cout << std::endl ;return 0;
}
| | | |