C++ Primer 学习中。

。。

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

find 、 find_if

/**********************线性查找O(n)
find();
find_if();
注意:
1.假设是已序区间,能够使用区间查找算法
2.关联式容器(set,map)有等效的成员函数find();时间复杂度O(log(n))
3.string 有等效的成员函数find();
**********************/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include<functional>
using namespace std; /*************************************************************************************
std::find algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value ); eg:
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
**************************************************************************************/ /*************************************************************************************
std::find_if algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );
eg:
template<class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
return first;
}
**************************************************************************************/
bool IsEven (int i);
int main ()
{
int myints[] = {10,30,20,40,20,10,30,40};
int * p; // pointer to array element:
p = find(myints,myints+8,30);
++p;
cout << "The element following 30 is " << *p << endl; vector<int> myvector (myints,myints+8);
vector<int>::iterator it; // iterator to vector element:
it = find (myvector.begin(), myvector.end(), 30);
++it;
cout << "The element following 30 is " << *it << endl; //输出第一个30---第二个30区间内的数
vector<int>::iterator it2;
it2=find (it,myvector.end(),30);
while(it!=it2)
cout<<*it++<<" ";
cout<<endl; /**--------------------------------find_if()---------------------------------**/
//找第一个偶数
it = find_if (myvector.begin(), myvector.end(), IsEven);//函数 或 函数对象
cout << "The first odd value is " << *it << endl; it2 = find_if(myvector.begin(),myvector.end(), not1(bind2nd(modulus<int>(),2)));
cout << "The first odd value is " << *it2 << endl;
/**--------------------------------关联容器---------------------------------**/
set<int> s(myvector.begin(),myvector.begin()+4);
cout<<"复杂度为O(log(n)),查找*s.find(40):= " << *s.find(40) << endl;
/**---------------------------------string----------------------------------**/
string st("AngelaBaby");
string::size_type pos = st.find("Baby");
if(pos != string::npos)
cout<<"find it!"<<endl;
else cout<<"not find it!"<<endl; pos = st.find("baby");
if(pos != string::npos)
cout<<"find it!"<<endl;
else cout<<"not find it!"<<endl;
return 0;
} bool IsEven (int i)
{
return ((i%2)==0);
} /******
Output:
The element following 30 is 20
The element following 30 is 20
20 40 20 10
The first odd value is 10
The first odd value is 10
复杂度为O(log(n)),查找*s.find(40):= 40
find it!
not find it!
******/

STL_算法_查找算法(find、find_if)的更多相关文章

  1. 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 ...

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

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

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

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

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

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

  5. 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 ...

  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_算法_查找算法(lower_bound、upper_bound、equal_range)

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

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

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

随机推荐

  1. cf359D Pair of Numbers

    Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find ...

  2. Visual Code 自定义插件安装位置

    修改快速方式通过传入方式启动 ,示例: D:\installed_green_soft\VSCode\Code.exe --extensions-dir "D:\installed_gree ...

  3. <编程精粹:编写高质量C语言代码> 读书笔记

    0.规则<The Elements of Programming Style><The Elements of Style> 1.假想的编译程序(1)使用编译器提供的所有的可选 ...

  4. Error querying database找不到数据库的错误可能发生的原因..

    这个问题纠结了大概两个小时.原因是这样的,我刚刚换了一台新的电脑,准备把以前电脑上自己搭建的小项目放到新电脑上面,用myeclipse引入项目之后,启动项目在浏览器跑起来.然后输入账号密码登录主页,报 ...

  5. 【Visual Studio】The project appears to be under source control, but the associated source control plug-in is not installed on this computer

    [问题描述]用 Visual Studio 2013打开一个项目时,出现下面错误: [问题原因]参考 http://codeverge.com/asp.net.web-forms/the-projec ...

  6. 转 Python爬虫入门一之综述

    转自: http://cuiqingcai.com/927.html 静觅 » Python爬虫入门一之综述 首先爬虫是什么? 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为 ...

  7. Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

    ubuntu上安装Apache2时出现错误 Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0: ...

  8. js-禁止微信内置浏览器调整字体大小

    js方法: (function(){      if(typeof WeixinJSBridge=="object"&& typeof WeixinJSBridge ...

  9. django中表变更后migrate无效的问题

    问题描述: 已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrat ...

  10. datetimepicker[jquery-ui]时间控件的三种初始化方法

    1.只显示年月日 $( ".datepicker").datepicker({ needDay:true, changeMonth: true, //显示月份 changeYear ...