C++ Primer 学习中。

。。

简单记录下我的学习过程 (代码为主)

//全部容器适用(O(log(n)))  
 已序区间查找算法



lower_bound()        //找第一个符合的元素,返回位置迭代器



upper_bound()        //找最后一个符合的元素。返回位置迭代器



equal_range()        //找一对迭代器pair(<>,<>)



关联式容器有等效的成员函数。性能更佳



#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; /*************************************************************************************
std::lower_bound 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class T>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value ); template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp ); //eg:
template <class ForwardIterator, class T>
ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (*it<value) // or: if (comp(*it,value)), for the comp version
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}
*************************************************************************************/ /*************************************************************************************
std::upper_bound 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class T>
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,
const T& value ); template <class ForwardIterator, class T, class Compare>
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp ); //eg:
template <class ForwardIterator, class T>
ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (!(value<*it)) // or: if (!comp(value,*it)), for the comp version
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}
*************************************************************************************/ /*************************************************************************************
std::equal_range 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T, class Compare>
pair<ForwardIterator,ForwardIterator>
equal_range ( ForwardIterator first, ForwardIterator last, const T& value,
Compare comp ); //eg:
template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range ( ForwardIterator first, ForwardIterator last, const T& value )
{
ForwardIterator it = lower_bound (first,last,value);
return make_pair ( it, upper_bound(it,last,value) );
}
*************************************************************************************/
bool mygreater (int i,int j){ return (i>j); } int main()
{
int myints[] = {10,20,30,30,20,10,10,20};
vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
vector<int>::iterator low,up; sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 cout<<"10 10 10 20 20 20 30 30\n"; low=lower_bound (v.begin(), v.end(), 20); // ^
up= upper_bound (v.begin(), v.end(), 20); // ^ cout << "20 lower_bound at position " << int(low- v.begin()) + 1 << endl;
cout << "20 upper_bound at position " << int(up - v.begin()) + 1 << endl; cout << endl; /**-------------------------------------------------------------------------------**/ pair<vector<int>::iterator,vector<int>::iterator> bounds; // using default comparison:
// sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
bounds=equal_range (v.begin(), v.end(), 20); // ^ ^
cout<<"10 10 10 20 20 20 30 30\n";
cout << "20 bounds at positions " << int(bounds.first - v.begin());
cout << " and " << int(bounds.second - v.begin()) << endl; // using "mygreater" as comp:
sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10
bounds=equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^ cout<<endl<<"30 30 20 20 20 10 10 10"<<endl; cout << "20 bounds at positions " << int(bounds.first - v.begin());
cout << " and " << int(bounds.second - v.begin()) << endl; return 0;
} /******
Output:
10 10 10 20 20 20 30 30
20 lower_bound at position 4
20 upper_bound at position 7 10 10 10 20 20 20 30 30
bounds at positions 3 and 6 30 30 20 20 20 10 10 10
bounds at positions 2 and 5 */

STL_算法_查找算法(lower_bound、upper_bound、equal_range)的更多相关文章

  1. cb34a_c++_STL_算法_查找算法_(7)_lower_bound

    cb34a_c++_STL_算法_查找算法_(7)_lower_bound//针对已序区间的查找算法,如set,multiset关联容器-自动排序lower_bound()--第一个可能的位置uppe ...

  2. cb32a_c++_STL_算法_查找算法_(5)adjacent_find

    cb32a_c++_STL_算法_查找算法_(5)adjacent_findadjacent_find(b,e),b,begin(),e,end()adjacent_find(b,e,p),p-par ...

  3. cb28a_c++_STL_算法_查找算法_(1)find_find_if

    cb28a_c++_STL_算法_查找算法_(1)find_find_iffind() //线性查找,比较慢.pos1 = find(ilist.begin(), ilist.end(), 5);fi ...

  4. cb33a_c++_STL_算法_查找算法_(6)binary_search_includes

    cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...

  5. cb31a_c++_STL_算法_查找算法_(4)find_first_of

    cb31a_c++_STL_算法_查找算法_(4)find_first_offind_first_of(b,e,sb,se),sb,second begin, se,second end();find ...

  6. cb30a_c++_STL_算法_查找算法_(3)search_find_end

    cb30a_c++_STL_算法_查找算法_(3)search_find_endsearch()pos = search(ideq.begin(), ideq.end(), ilist.begin() ...

  7. cb29a_c++_STL_算法_查找算法_(2)search_n

    cb29a_c++_STL_算法_查找算法_(2)search_n//比如:连续查找连续的n个8search_n(b,e,c,v),迭代器b,begin(),e,end().连续的c个vpos=sea ...

  8. STL_算法_查找算法(binary_search、includes)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n)))     已序区间查找算法 binary_search             //二分查 ...

  9. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

随机推荐

  1. Strom优化指南

    摘要:本文主要讲了笔者使用Strom中的一些优化建议 1.使用rebalance命令动态调整并发度 Storm计算以topology为单位,topology提交到Storm集群中运行后,通过storm ...

  2. ionic 项目中 使用 sass

    注: 1.先安装node-sass  -->> npm install --save node-sass --registry=https://registry.npm.taobao.or ...

  3. plsql 连接oracle数据库的2种方式

      plsql 连接oracle数据库的2种方式 CreationTime--2018年8月10日09点50分 Author:Marydon 方式一:配置tnsnames.ora 该文件在instan ...

  4. 像烟瘾一样的Adobe Flash,真的戒不掉吗?

    近来对Adobe Flash来说真是段难过的日子.Hacking Team公司外泄的440GB电子邮件数据已成为黑客挖掘安全漏洞的宝藏. 光是Flash就被发现了三个不同的漏洞: l  CVE-201 ...

  5. DIV布局之position详解

    相对定位和绝对定位 定位标签:position 包含属性:relative(相对) absolute(绝对) 1.position:relative; 如果对一个元素进行相对定位,首先它将出现在它所在 ...

  6. iOS升级swift3 遇到Overriding non-open instance method outside of its defining module的解决方案

    最近将我之前的一个swift项目升级swift3,说多了都是泪... 其中,遇到这样一个错误: 这是用的三方:ENSwiftSideMenu时引出的 报了两个错: 1.Cannot inherit f ...

  7. 如何发布开源自己的框架或类库到CocoaPods - 图文讲解

    当自己的库已经上传GitHub后,那么如何快速简单的开源自己的库呢? 这里就是介绍如何将自己的类库上传到pods管理库,以便开源所有人都能方便使用. 准备前提: - 项目已上传到GitHub (注意, ...

  8. 微信小游戏“跳一跳”,Python“外挂”已上线

    微信又一次不声不响地搞了个大事情: “小游戏”上线了! 于是,在这辞旧迎新的时刻,毫无意外的又火了. 今天有多少人刷了,让我看到你们的双手! 喏,我已经尽力了…… 不过没关系,你们跳的再好,在毫无心理 ...

  9. Launchy – 快速调出你的程序

    http://www.appinn.com/launchy/这个可是今天绝对的发现,超级好玩好用的程序,Launchy 可以使你快速的调用你电脑中的程序快捷方式,而且很智能,使用起来很智能,安装后用A ...

  10. Linux-Tmux使用初体验

    Tmux使用初体验 tmux #开启tmux tmux ls #显示已有tmux列表(ctrl+b s) tmux attach-session -t 数字 #选择tmux ctrl+b c 创建一个 ...