STL中的二分查找函数
2017-11-08 20:52
281 查看
头文件 <algorithm>
查找函数可以分为两类
① count find 用于非有序序列的查找,On的复杂度。
② binary_search upper_bound lower_bound equal_range 用于有序序列的查找,Ologn的复杂度。
①
count(num,num+n,w);
count(v.begin(),v.end(),w);
返回值是一个数,代表从起始到结束位置数w的个数。
find(num,num+n,w);
find(v.begin(),v.end(),w);
返回值是一个指针,若找到w,则指向找到的第一个w,若找不到,则指向队尾元素的下一个位置。
可以在函数后面加-num、-v.begin() 使其返回下标。
②
binary_search(num,num+n,w);
binary_search(v.begin(),v.end(),w);
返回值是一个布尔类型变量,找到为true,否则为false。
lower_bound(num,num+n,w);
lower_bound(v.begin(),v.end(),w);
返回值是一个指针,指向第一个大于等于w的数。
即:若w存在,则指向第一个w,若w不存在,则指向第一个大于w的数,若所有数均小于w,则指向队尾的后一个位置。
同样可以在函数后面加-num、-v.begin() 使其返回下标。
upper_bound(num,num+n,w);
upper_bound(v.begin(),v.end(),w);
返回值是一个指针,指向第一个大于w的数。
即:不管w存在还是不存在,都指向第一个大于w的数,若全部小于w,则指向队尾的后一个位置。
同样可以在函数后面加-num、-v.begin() 使其返回下标。
equal_range (num,num+n,w);
equal_range (v.begin(),v.end(),w);
返回值是pair类型,其中第一个值是lower_bound的返回值,第二个是upper_bound的返回值
查找函数可以分为两类
① count find 用于非有序序列的查找,On的复杂度。
② binary_search upper_bound lower_bound equal_range 用于有序序列的查找,Ologn的复杂度。
①
count(num,num+n,w);
count(v.begin(),v.end(),w);
返回值是一个数,代表从起始到结束位置数w的个数。
find(num,num+n,w);
find(v.begin(),v.end(),w);
返回值是一个指针,若找到w,则指向找到的第一个w,若找不到,则指向队尾元素的下一个位置。
可以在函数后面加-num、-v.begin() 使其返回下标。
②
binary_search(num,num+n,w);
binary_search(v.begin(),v.end(),w);
返回值是一个布尔类型变量,找到为true,否则为false。
lower_bound(num,num+n,w);
lower_bound(v.begin(),v.end(),w);
返回值是一个指针,指向第一个大于等于w的数。
即:若w存在,则指向第一个w,若w不存在,则指向第一个大于w的数,若所有数均小于w,则指向队尾的后一个位置。
同样可以在函数后面加-num、-v.begin() 使其返回下标。
upper_bound(num,num+n,w);
upper_bound(v.begin(),v.end(),w);
返回值是一个指针,指向第一个大于w的数。
即:不管w存在还是不存在,都指向第一个大于w的数,若全部小于w,则指向队尾的后一个位置。
同样可以在函数后面加-num、-v.begin() 使其返回下标。
equal_range (num,num+n,w);
equal_range (v.begin(),v.end(),w);
返回值是pair类型,其中第一个值是lower_bound的返回值,第二个是upper_bound的返回值
相关文章推荐
- 有关STL的二分查找函数的理解
- stl中二分查找相关的函数
- STL中二分查找函数
- C++ STL 二分查找函数(lower_bound 和 upper_bound)
- STL中与二分查找相关的4个函数
- STL中与二分查找相关的4个函数(转)
- STL中二分查找函数
- STL(1)——查找函数find的使用
- Codeforces Round #251 Div2D 439D Devu and his Brother 二分查找STL
- STL之二分查找 (Binary search in STL)
- STL之二分查找 (Binary search in STL)
- STL中的二分查找(binary_search)
- STL中的二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL之二分查找 (Binary search in STL)
- 用函数写二分查找
- 有关二分查找的STL
- STL中的二分查找———lower_bound,upper_bound,binary_search
- STL中二分查找相关算法(binary_search, lower_bound, upper_bound, equal_range)
- STL中的二分查找
- STL 中 lower_bound 与 upper_bound 与 二分查找