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!
******/
时间: 2024-08-05 02:55:49

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

STL_算法_查找算法(lower_bound、upper_bound、equal_range)

C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的元素,返回位置迭代器 upper_bound()        //找最后一个符合的元素.返回位置迭代器 equal_range()        //找一对迭代器pair(<>,<>) 关联式容器有等效的成员函数.性能更佳 #include<iostream> #incl

算法----二分查找算法

二分查找算法是在有序数组中用到的较为频繁的一种算法,在未接触二分查找算法时,最通用的一种做法是,对数组进行遍历,跟每个元素进行比较,其时间为O(n).但二分查找算法则更优,因为其查找时间为O(lgn),譬如数组{1, 2, 3, 4, 5, 6, 7, 8, 9},查找元素6,用二分查找的算法执行的话,其顺序为: 1.第一步查找中间元素,即5,由于5<6,则6必然在5之后的数组元素中,那么就在{6, 7, 8, 9}中查找, 2.寻找{6, 7, 8, 9}的中位数,为7,7>6,则6应该在7

面试常见算法-排序查找算法

算法是程序员必被的一个技能,在面试中常常出现,下面总结了面试中出现的常见算法,这些算法程序员应该牢记在心中,要非常熟练. 插入排序算法 原理:将数组分为无序区和有序区两个区,然后不断将无序区的第一个元素按大小顺序插入到有序区中去,最终将所有无序区元素都移动到有序区完成排序. 要点:设立哨兵,作为临时存储和判断数组边界之用. public class InsertSort { private static void insertSort(int[] a) { int j; int tmp; for

PHP的排序算法跟查找算法

排序算法: (1)冒泡排序 1 $arr = array(15,8,20,50,37,85,10,5,11,4); 2 //冒泡排序 3 function maoPao($arr){ 4 for($i = 0; $i < count($arr)-1; $i++){ 5 for($j = 0; $j < count($arr)-1; $j++){ 6 if($arr[$j] > $arr[$j+1]){ 7 $temp = $arr[$j]; 8 $arr[$j] = $arr[$j+1]

排序算法和查找算法

示例:分别用冒泡排序,快速排序,选择排序,插入排序将数组中的值从小到大的顺序排序 $array = (9,5,1,3,6,4,8,7,2); 1.冒泡排序算法 //思路:两两比较待排序数据元素的大小,发现两个数据元素的次序相反即进行交换,直到没有反序的数据元素为止 function bubbleSort($array){         $lg = count($array);         if($lg <=1){             return $array;         }  

JS中数据结构之检索算法(查找算法)

顺序查找 查找指定值 function seqSearch(arr, data) { for (var i = 0; i < arr.length; ++i) { if (arr[i] == data) { return true; } } return false; } 查找最小值和最大值 function findMin(arr) { var min = arr[0]; for (var i = 1; i < arr.length; ++i) { if (arr[i] < min)

数据结构和算法--7查找算法

1.常用的查找算法 1) 顺序(线性)查找 2) 二分查找/折半查找 3) 插值查找 4) 斐波那契查找 2.查找 1)线性查找 A.题目: 有一个数列[1,43,22,-10,0],判断数列中是否包含此名称,如果找到了,就提示找到,并给出下标值. B,思路: 逐一查找 C.代码 package com.offcn.search; //线性查找 public class SeqSearch { public static void main(String[] args){ int[] arr =

Java学习资料-Java常用算法-二分查找算法

binarySearch源程序 public class binarySearch { public static int binarySearch(int[] dataset ,int data) { int beginIndex = 0; //定义起始位置 int endIndex = dataset.length - 1;  //定义结束位置 int midIndex = -1; //定义中点 if(data <dataset[beginIndex]||data>dataset[endI

复习常用算法_冒泡算法

package com.itemuch.cloud.test; import java.io.Serializable;import java.util.Date;import java.util.HashMap;import java.util.Map; public class javaBasical{ public static void main(String arsg[]){ int[] unSortNums = new int[]{ 1,3,4,5,2,9}; getSortResu