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. Python基础教程总结(一)

    引言: 一直都听说Python很强大,以前只是浏览了一些博客,发现有点像数学建模时使用的Matlab,就没有深入去了解了.如今Python使用的地方越来越多,最近又在学习机器学习方面的知识,因此想系统 ...

  2. spring和resteasy 的集成方式

    spring和resteasy集成,三种主要的方式, 对于和jboss as7的集成不需要做任何工作,jboss默认集成了resteasy,只需要对业务pojo做一些jax-rs的注解标注即可.这里讲 ...

  3. 优先队列priority_queue的使用

    优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序. 每次的push和pop操作,队列都会动态的调整,以达到我们预期的方式来存储.例如:我们常用的操作就 ...

  4. Servlet 2.4 规范之第五篇:请求

    request对象封装了来自客户端的所有请求信息.在HTTP协议中,客户端发给服务端的所有信息都是通过request对象的请求头和请求体来传送的.           SRV.4.1    HTTP协 ...

  5. Android内存泄漏总结

    内存泄漏问题老生常谈,很常见也很难根治,今天我在这里总结一下内存泄漏的原因和解决方法: 所谓内存泄漏,就是本该被回收的对象,由于某些原因不能被回收,继续占用堆内存的这种状态,导致的结果也是显而易见的, ...

  6. Codeforces Gym101502 A.Very Hard Question

    2017 JUST Programming Contest 3.0 昨天的训练赛,打的好难过,因为被暴打了,写了8题,他们有的写了9题,差了一道dp,博客上写7道题的题解. 因为有一道是套板子过的,并 ...

  7. Codeforces 597B Restaurant(离散化 + 贪心)

    题目链接 Restaurant 题目意思就是在$n$个区间内选出尽可能多的区间,使得这些区间互不相交. 我们先对这$n$个区间去重. 假如有两个区间$[l1, r1],[l2, r2]$ 若满足$l1 ...

  8. springboot2.x整合redis实现缓存(附github链接)

    本文代码已提交github:    https://github.com/LCABC777/Springboot-redis(1)Springboot中使用redis操作的两种方式:lettuce和j ...

  9. OS | Socket

    TCP 创建socket: int socket(int domain, int type, int protocol); AF = Address FamilyPF = Protocol Famil ...

  10. 猴子都能懂的git教程链接

    http://backlogtool.com/git-guide/cn/intro/intro1_1.html