std::find:

查找容器元素, find仅仅能查找容器元素为<基本数据类型>

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. int main()
  5. {
  6. std::vector<int> v;
  7. for (int i = 0; i < 10; ++i)
  8. v.push_back(i);
  9. std::vector<int>::iterator iter = std::find(v.begin(), v.end(), 3);
  10. if (iter == v.end())
  11. std::cout << "can not find value 3 in v" << std::endl;
  12. else
  13. std::cout << "the index of value " << (*iter) << " is " << std::distance(v.begin(), iter) << std::endl;
  14. return 0;
  15. }

std::find_if

按条件查找容器元素, 容器类型为<类>时, 无法使用find来查找,  所以要使用find_if来查找

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <functional>
  5. struct Point
  6. {
  7. int x;
  8. int y;
  9. };
  10. struct PointFindByCoord : public std::binary_function<Point, Point, bool>
  11. {
  12. bool operator () (const Point &obj1, const Point &obj2) const
  13. {
  14. return obj1.x == obj2.x && obj1.y == obj2.y;
  15. }
  16. };
  17. int main()
  18. {
  19. std::vector<Point> v;
  20. for (int i = 0; i < 5; ++i)
  21. {
  22. for (int j = 0; j < 5; ++j)
  23. {
  24. Point pt;
  25. pt.x = i;
  26. pt.y = j;
  27. v.push_back(pt);
  28. }
  29. }
  30. Point needFind;
  31. needFind.x = 4;
  32. needFind.y = 3;
  33. std::vector<Point>::iterator iter = std::find_if(v.begin(), v.end(), std::bind2nd(PointFindByCoord(), needFind));
  34. if (iter == v.end())
  35. {
  36. // 未找到
  37. }
  38. else
  39. std::cout << "the index of value Point(" << (*iter).x << ", " << (*iter).y
  40. << ") is " << std::distance(v.begin(), iter) << std::endl;
  41. return 0;
  42. }

转会:http://blog.csdn.net/ilysony/article/details/6526545

c++ stl algorithm: std::find, std::find_if的更多相关文章

  1. c++ stl algorithm: std::fill, std::fill_n

    std::fill 在[first, last)范围内填充值 #include <iostream> #include <vector> #include <algori ...

  2. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  3. STL algorithm 头文件下的常用函数

    algorithm 头文件下的常用函数 1. max(), min()和abs() //max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须时两个(可以是浮点数) //返回3 ...

  4. STL algorithm算法merge(34)

    merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...

  5. STL algorithm算法is_permutation(27)

    is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...

  6. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  7. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  8. C++中std::fill/std::fill_n的使用

    There is a critical difference between std::fill and memset that is very important to understand. st ...

  9. std:: lower_bound std:: upper_bound

    std:: lower_bound 该函数返回范围内第一个不小于(大于或等于)指定val的值.如果序列中的值都小于val,则返回last.序列应该已经有序! eg: #include <iost ...

随机推荐

  1. UVA 5875 DP

    题意:给你一堆二维点,每个点有一些分数. 现在要从点(0 , 0 )出发,只能从标号小的点走到大的点,每个人有一个走的距离的限制,问最后能拿到的最高的分数,当然这个人从(0 , 0)出发还得回到( 0 ...

  2. Linq 数据操作,两个数组求差、交集、并集

    int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = { 4, 5, 6, 7, 8, 9, 10 }; int[] c = { 1, 2, 3, 3, 4, 1, ...

  3. 如何收集 EBS 各种相关业务的表的数据

    1. Receiving 相关 參照 Note: 402245.1, 跑 rcv11i_sa.sql 就能够, 输入 po number, 其余默认. 參照 Note: 1294177.1, 假设上面 ...

  4. CentOS修改yum更新源

    1. 在修改前先备份该文件 cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 2. 修改更新源配置文 ...

  5. VC++ 在两个文件互相包含时会出现的错误

    首先,要分别在两个文件中实现以下两个类 class Object { public: NewType ToType(); }; class NewType : public Object { } -- ...

  6. HTML5特性检測

    HTML5特性检測:    1.检測全局对象:诸如window或navigator是否拥有特定的属性    2.创建元素:检測该元素的DOM对象是否拥有特定的属性    3.创建元素:检測该元素的DO ...

  7. 同时显示多个 Notification

    主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block 企业库数据库访问模块通过抽象工厂模式,允许用户 ...

  9. 它们的定义AlertDialog(二)

    先来看主页面布局 main_activity.xml里面仅仅有一个button(加入点击事件.弹出载入框) 再看MainActivity package com.example.loadingdial ...

  10. POJ 2240 Arbitrage(最短路 套汇)

    题意  给你n种币种之间的汇率关系  推断是否能形成套汇现象  即某币种多次换为其他币种再换回来结果比原来多 基础的最短路  仅仅是加号换为了乘号 #include<cstdio> #in ...