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. Servlet:通过初始参数实现权限访问某个文件、页面

    目录结构 src 目录下com.xieyuan包MyServlet.java文件(Servlet文件) package com.xieyuan; import java.awt.Color; impo ...

  2. FOJ 1591 —— Coral的烦恼

    #include<stdio.h> int main() { __int64 n,i,sum,l,r; while(scanf("%I64d",&n)!=EOF ...

  3. Windows内核

    每天我们都在使用Windows系统学习.编程.听音乐.玩游戏,Windows的操作想来是非常熟练了,但是你又对Windows究竟了解多少呢?本系列的目的,就是让你对Windows系统有个更直观.更清楚 ...

  4. jQuery中的getJSON()

    json文件是一种轻量级的数据交互格式.一般在jQuery中使用getJSON()方法读取. $.getJSON(url,[data],[callback]) url:json文件地址 data:可选 ...

  5. Oracle 最简单的随系统自己主动启动

    Oracle 最简单的随系统自己主动启动 俗话说用户是上帝,他们有时候提出一个问题很的简单,就仅仅须要一句话,一分钟就完事了.可是拿到我们DBA来说,可能至少得半个小时甚至半个月才干满足他的一句话.有 ...

  6. POJ 2524 :Ubiquitous Religions

    id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...

  7. NPC

    这里的想说的NPC不是Non-Player-Controled,非玩家控制角色,而是Non-determinisitc Polynomial complete problem,它属于一类很特殊的问题, ...

  8. SWT的TableVierer的使用三(数据筛选和着色)

    如果我们想根据某一列来过滤记录,如何实现呢?很简单,定义一个过滤器filter.这里只演示定义一个过滤器的情况.现实中你可以定义多个灵活的过滤器,通过替换过滤器来实现各种各样的过滤.一.过滤器代码: ...

  9. 在自己的base脚本中实现自动补全

    在90年代Linux和DOS共存的年代里,Linux的Shell们有一个最微不足道但也最实用的小功能,就是命令自动补全.而DOS那个笨蛋一直到死都没学会什么叫易用. Linux的这个微不足道的小传统一 ...

  10. python战斗2:看到一个页面编码

    编程环境:虚拟linux (windows 下 cygwin) 识别网页编码. usage: python coding http://www.***.com 測试结果: watermark/2/te ...