C++ STL: lower_bound 和 upper_bound
生活随笔
收集整理的這篇文章主要介紹了
C++ STL: lower_bound 和 upper_bound
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
接口聲明
以下有兩個不同的版本
lower_bound
-
template <class ForwardIterator, class T>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val); -
template <class ForwardIterator, class T, class Compare>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);
upper_bound
-
template <class ForwardIterator, class T>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val); -
template <class ForwardIterator, class T, class Compare>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);
兩個接口都是返回一個迭代器,且改迭代器指向一個非遞減序列[first,last)內(nèi)部 的一個元素地址,后續(xù)通過該迭代器繼續(xù)訪問剩余元素。
兩個接口的主要區(qū)別是:
lower_bound 指向的是第一個大于等于val的位置
upper_bound 指向的是第一個大于val的位置
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-6B3E9GCI-1592484277761)(/Users/zhanghuigui/Library/Application Support/typora-user-images/image-20200618111744296.png)]
接口源碼實現(xiàn)
Lower_bound
// function : lower_bound
/// @brief Returns an iterator pointing to the first element in the range
/// [first, last) that is not less than (i.e. greater or equal to) val.
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found
//-----------------------------------------------------------------------------
// 返回大于等于val的數(shù)據(jù)
template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t lower_bound(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt = Filter())
{assert((last - first) >= 0);if (last == first) return last;Iter_t itaux = internal_find_first(first, last, val, comp, flt);return (itaux == (last - 1) and comp(flt(*itaux), val)) ? last : itaux;
};//-----------------------------------------------------------------------------
// function : internal_find_first
/// @brief find if a value exist in the range [first, last).
/// Always return as valid iterator in the range [first, last-1]
/// If exist return the iterator to the first occurrence. If don't exist
/// return the first greater than val.
/// If val is greater than the *(last-1), return (last-1)
/// If val is lower than (*first), return first
//
/// @param [in] first : iterator to the first element of the range
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found,
//-----------------------------------------------------------------------------
// 使用internal function進行二分查找
template <class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t internal_find_first(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt = Filter())
{Iter_t LI = first, LS = last - 1, it_out = first;while (LI != LS){it_out = LI + ((LS - LI) >> 1);if (comp(flt(*it_out), val))LI = it_out + 1;else LS = it_out;};return LS;
};
Upper bound
// function :upper_bound
/// @brief return the first element greather than val.If don't exist
/// return last
//
/// @param [in] first : iterator to the first element of the range
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found
/// @remarks
//-----------------------------------------------------------------------------
//返回大于val的數(shù)據(jù)
template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t upper_bound(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt = Filter())
{assert((last - first) >= 0);if (last == first) return last;Iter_t itaux = internal_find_last(first, last, val, comp, flt);return (itaux == first and comp(val, flt(*itaux))) ? itaux : itaux + 1;
}// function : internal_find_last
/// @brief find if a value exist in the range [first, last).
/// Always return as valid iterator in the range [first, last-1]
/// If exist return the iterator to the last occurrence.
/// If don't exist return the first lower than val.
/// If val is greater than *(last-1) return (last-1).
/// If is lower than the first, return first
//
/// @param [in] first : iterator to the first element of the range
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found, if not found return last//-----------------------------------------------------------------------------
template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t internal_find_last(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt =Filter())
{Iter_t LI = first, LS = last - 1, it_out = first;while (LI != LS){it_out = LI + ((LS - LI + 1) >> 1);if (comp(val, flt(*it_out))) LS = it_out - 1;else LI = it_out;};return LS;
};
應(yīng)用
// lower_bound/upper_bound example
#include <iostream> // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vectorint main () {int myints[] = {10,20,30,30,20,10,10,20};std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30std::vector<int>::iterator low,up;low=std::lower_bound (v.begin(), v.end(), 20); // ^up= std::upper_bound (v.begin(), v.end(), 20); // ^std::cout << "lower_bound at position " << (low- v.begin()) << '\n';std::cout << "upper_bound at position " << (up - v.begin()) << '\n';return 0;
}
輸出如下
lower_bound at position 3 # 第一個大于等于20的位置
upper_bound at position 6 # 第一個大于20的位置
總結(jié)
以上是生活随笔為你收集整理的C++ STL: lower_bound 和 upper_bound的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大家帮我看看宋茜背的这个包包是什么牌子,
- 下一篇: “六年春二月”上一句是什么