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. java面试题之@Autowired和@Resource的区别

    @Autowired和@Resource的区别: 1.都可以用来装配bean,都可以写在字段或者方法上: 2. @Autowired默认按类型装配,默认情况下必须要求依赖对象必须存在,如果允许为nul ...

  2. SpringBoot基础之MockMvc单元测试

    SpringBoot创建的Maven项目中,会默认添加spring-boot-starter-test依赖.在<5分钟快速上手SpringBoot>中编写的单元测试使用了MockMvc.本 ...

  3. poj 1430 Binary Stirling Number 求斯特林数奇偶性 数形结合| 斯特林数奇偶性与组合数的关系+lucas定理 好题

    题目大意 求子集斯特林数\(\left\{\begin{matrix}n\\m\end{matrix}\right\}\%2\) 方法1 数形结合 推荐一篇超棒的博客by Sdchr 就是根据斯特林的 ...

  4. 【CF1027C】Minimum Value Rectangle(贪心,数学)

    题意:给定n根木棍,不允许拼接或折断,选择四根组成矩形,求所有合法矩形中周长平方与面积比最小的一个,输出拼成这个矩形的四根木棍 n<=1e6 思路:猜结论:答案必定从相邻的4根中产生 证明见ht ...

  5. Spring入门 (IOC)

    1.实现原理

  6. C# DataSet与DataTable的区别和用法

    DataSet是数据集,DataTable是数据表,DataSet存储多个DataTable.DataSet和DataTable像是专门存储数据的一个容器,在你查询数据库得到一些结果时可以存在里面. ...

  7. js -“=”“==”和“===”的区别

    这个问题再面试中经常被问到,说实话我都是懵的,一个“=”和两个“==”等的区别我还是知道的,就是三个“===”我完全是不知道的,因为我基本上都没有遇到过且用到过,所以再这个问题上我是没分的,人家考官就 ...

  8. (1)Unity3d界面、入门

    项目视图 层级视图 属性视图 场景视图 游戏视图 调整u3d整体界面布局 1.查看和移动视图 快捷键Q 2.沿轴方向位移 快捷键W 3.沿轴向旋转 快捷键E 4.沿轴向缩放 快捷键R 5.自由调节小大 ...

  9. Codeforces 875C National Property(拓扑排序)

    题目链接  National Property 给定n个单词,字符集为m 现在我们可以把其中某些字母变成大写的.大写字母字典序大于小写字母. 问是否存在一种方案使得给定的n个单词字典序不下降. 首先判 ...

  10. ABP开发框架前后端开发系列---(2)框架的初步介绍

    在前面随笔<ABP开发框架前后端开发系列---(1)框架的总体介绍>大概介绍了这个ABP框架的主要特点,以及介绍了我对这框架的Web API应用优先的一些看法,本篇继续探讨ABP框架的初步 ...