6.搜索
1.binary_search()
在已序区间搜寻指定的元素,【搜索区间必须是有序的】
函数原型为:
template<class ForwardIt, class T>
bool binary_search(ForwardIt first, ForwardIt last, const T& value)
{
first = std::lower_bound(first, last, value);//第一个不小于value的值
return (first != last && !(value < *first));
}
//第二种形式
template<class ForwardIt, class T, class Compare>
bool binary_search(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
first = std::lower_bound(first, last, value);
return (first != last && !(comp(value, *first));
}
说明:
1. 检查已排序的范围[first, last)内是否包含一个元素等于value。
2. 第一种形式:使用运算符operator<比较元素
3. 第二种形式: 使用二元比较函数: comp
4. 返回值:找到返回True,否则false
2.includes()
用于检查源区间中若干值是否存在。
函数原型:
//版本一
template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2)
{
for (; first2 != last2; ++first1)//检查序列2 和序列一的匹配情况
{
if (first1 == last1 || *first2 < *first1)
return false;
if ( !(*first1 < *first2) )//两个对应的元素相等
++first2;
}
return true;
}
//版本二
template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, Compare comp)
{
for (; first2 != last2; ++first1)
{
if (first1 == last1 || comp(*first2, *first1))
return false;
if (!comp(*first1, *first2))
++first2;
}
return true;
}
说明:
1. 第一种形式:默认比较operator<
2. 第二种形式: 二元比较函数:comp
3. 返回值: True,false
3.搜索第一个和最后一个可能的位置
- lower_bound():返回第一个“大于等于value”的值的位置
- equal_range(): 返回的是lower_bound()和upper_bound()的共同值
- upper_bound():返回第一个不大于value的值的位置
1.lower_bound()
函数原型:
//版本一
template<class ForwardIt, class T>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value)
{
ForwardIt it;
std::iterator_traits<ForwardIt>::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;
} else count = step;
}
return first;
}
//版本二
template<class ForwardIt, class T, class Compare>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
ForwardIt it;
std::iterator_traits<ForwardIt>::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;
} else count = step;
}
return first;
}
说明:
1. 第一种形式:默认比较operator<
2. 第二种形式: 二元比较函数:comp
3. 返回值:如果存在大于等于value的第一个值,返回他的位置的迭代器,否则返回end()迭代器
2.upper_bound()
函数原型:
//版本一
template<class ForwardIt, class T>
ForwardIt upper_bound(ForwardIt first, ForwardIt last,
const T& value)
{
ForwardIt it;
std::iterator_traits<ForwardIt>::distance_type count, step;
count = std::distance(first,last);
while (count > 0) {
it = first;
step = count / 2;
std::advance(it, step);
if (!(value < *it)) {
first = ++it;
count -= step + 1;
} else count = step;
}
return first;
}
//版本二
template<class ForwardIt, class T, class Compare>
ForwardIt upper_bound(ForwardIt first, ForwardIt last,
const T& value, Compare comp)
{
ForwardIt it;
std::iterator_traits<ForwardIt>::distance_type count, step;
count = std::distance(first,last);
while (count > 0) {
it = first;
step = count / 2;
std::advance(it, step);
if (!comp(value, *it)),
first = ++it;
count -= step + 1
} else count = step;
}
return first;
}
说明:
1. 第一种形式:默认比较operator<
2. 第二种形式: 二元比较函数:comp
3. 返回值:如果存在大于value的第一个值,返回他的位置的迭代器,否则返回end()迭代器
3.equal_range()
返回一个pair类型,第一个成员是指向不小于value的第一个元素的迭代器,第二个成员是指向大于value的第一个元素的迭代器。
函数原型:
//版本一
template<class ForwardIt, class T
std::pair<ForwardIt,ForwardIt>
equal_range(ForwardIt first, ForwardIt last,
const T& value)
{
return std::make_pair(std::lower_bound(first, last, value),
std::upper_bound(first, last, value));
}
//版本二
template<class ForwardIt, class T, class Compare>
std::pair<ForwardIt,ForwardIt>
equal_range(ForwardIt first, ForwardIt last,
const T& value, Compare comp);
{
return std::make_pair(std::lower_bound(first, last, value, comp),
std::upper_bound(first, last, value, comp));
}
例子:
------------------省略--------------
vector<int> vec1 = {1,2,3,4,5,6,6,7};
pair<vector<int>::iterator, vector<int>::iterator> eq;
eq = equal_range(vec1.begin(), vec1.end(), 4);
------------------省略--------------
结果为:
vec1: 1 2 3 4 5 6 6 7
lower: 4 upper: 5