C++11标准模板(STL)- 算法(std::lower_bound)
创始人
2024-02-16 17:43:20
0
定义于头文件 

算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

返回指向第一个不小于给定值的元素的迭代器

std::lower_bound

template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );

(1)(C++20 前)

template< class ForwardIt, class T >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );

(C++20 起)

template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );

(2)(C++20 前)

template< class ForwardIt, class T, class Compare >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );

(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 的)类型 Type1Type2 的值,无关乎值类别(从而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起))。
类型 Type1 必须使得 ForwardIt 类型的对象能在解引用后隐式转换到 Type1 。类型 Type2 必须使得 T 类型的对象能隐式转换到 Type2 。 ​

类型要求
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
- Compare 必须满足二元谓词 (BinaryPredicate) 的要求。不要求满足 比较 (Compare) 。

返回值

指向首个不小于 value 的元素的迭代器,或若找不到这种元素则为 last

复杂度

进行的比较次数与 firstlast 间的距离成对数(至多 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;
}

 输出

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...