STL_算法_查找算法(find、find_if)
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)的更多相关文章
- 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 ...
- cb28a_c++_STL_算法_查找算法_(1)find_find_if
cb28a_c++_STL_算法_查找算法_(1)find_find_iffind() //线性查找,比较慢.pos1 = find(ilist.begin(), ilist.end(), 5);fi ...
- cb34a_c++_STL_算法_查找算法_(7)_lower_bound
cb34a_c++_STL_算法_查找算法_(7)_lower_bound//针对已序区间的查找算法,如set,multiset关联容器-自动排序lower_bound()--第一个可能的位置uppe ...
- cb33a_c++_STL_算法_查找算法_(6)binary_search_includes
cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...
- 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 ...
- cb30a_c++_STL_算法_查找算法_(3)search_find_end
cb30a_c++_STL_算法_查找算法_(3)search_find_endsearch()pos = search(ideq.begin(), ideq.end(), ilist.begin() ...
- 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 ...
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n))) 已序区间查找算法 lower_bound() //找第一个符合的 ...
- STL_算法_查找算法(binary_search、includes)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n))) 已序区间查找算法 binary_search //二分查 ...
随机推荐
- iOS-多线程(2)
多线程之NSOperation: 多线程的优点: 使用线程可以把占据时间长的程序中的任务呀放到后台去处理 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显 ...
- java面试题之哨兵如何判断主服务器是否下线?
通过流言协议来接收关于主服务器是否下线的信息,并使用投票协议来决定是否执行自动故障迁移,以及选择哪个从服务器作为新的主服务器.
- java面试题之你了解守护线程吗?它和非守护线程有什么区别
程序运行完毕,jvm会等待非守护线程完成后关闭,但是jvm不会等待守护线程. 守护线程最典型的的例子是:GC线程
- 【前端学习笔记】2015-09-09~~~~nodejs中的require()和module.exports
nodejs中一个js文件就可以看做是一个模块 在node环境中,可以直接var a=require('模块路径以及不带扩展名的模块名') exports---module.exports 其中nod ...
- sqlite 使用
'''SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说 没有独立的维护进程,所有的维护都来自于程序本身. 在python中,使用sqlite3创建数据库的连接,当我们指定的数据库文件不 ...
- LeetCode OJ——Unique Binary Search Trees II
http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 一题要求得出所有树的种类数,二题要求得出所有树. 在一题的基础上修改代码, ...
- CSS兼容IE6 IE7 IE8 IE9 Firefox的总结
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 拼题 L2-001 紧急救援 最短路计数+记录路径
https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 L2-001 紧急救援 (25 分) 作 ...
- Codeforces 895E Eyes Closed(线段树)
题目链接 Eyes Closed 题意 两个人玩一个游戏,现在有两种操作: 1.两个人格子挑选一个区间,保证两个的区间不相交.在这两个区间里面各选出一个数,交换这两个数. 2.挑选一个区间,求这个 ...
- codevs——2181 田忌赛马
2181 田忌赛马 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 中国古代的历史故事“田忌赛马”是为大 ...