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图像处理库PIL从入门到精通

    https://blog.csdn.net/column/details/pythonpil.html 示例: from PIL import Image import pytesseract pyt ...

  2. hud 4746 莫比乌斯反演

    Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others)Total S ...

  3. C和C++内存分配方式记录

    C. C++中内存分配方式可以分为三种: (1)从静态存储区域分配:内存在程序编译时就已经分配好,这块内存在程序的整个运行期间都存在.速度快.不容易出错,因为有系统会善后.例如全局变量,static变 ...

  4. 重复造轮子之RSA算法(一) 大素数生成

    出于无聊, 打算从头实现一遍RSA算法 第一步, 大素数生成 Java的BigInteger里, 有个现成的方法 public static BigInteger probablePrime(int ...

  5. Codeforces Gym101502 K.Malek and Summer Semester

    K. Malek and Summer Semester   time limit per test 1.0 s memory limit per test 256 MB input standard ...

  6. 洛谷——P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  7. codeforces A. Wrong Subtraction

    A. Wrong Subtraction time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. CODECHEF Oct. Challenge 2014 Children Trips

    @(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...

  9. javascript --- 原型继承与属性拷贝的综合应用

    对于继承来说,主要目标就是将一些现有的功能据为己有.也就是说,我们在新建一个对象的时候,通常首先继承现有对象,然后再为其添加额外的属性和方法. 对此,我们可以通过一个函数调用来完成. 具体而言就是: ...

  10. 在C#的数据类型中,什么属于值类型,什么属于引用类型

    转自原文 在C#的数据类型中,什么属于值类型,什么属于引用类型 类型:整数,浮点数,高精度浮点数,布尔,字符,结构,枚举引用类型:对象(Object),字符串,类,接口,委托,数组除了值类型和引用类型 ...