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. Java程序猿学习当中各个阶段的建议

    回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议   引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的 ...

  2. zoj3471(状压dp)

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题意:不超过10种气体,两两之间相互碰撞可以产生一定的能量,如 ...

  3. HDOJ 2736 Surprising Strings

    Surprising Strings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. [ACM] hdu 4418 Time travel (高斯消元求期望)

    Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...

  5. Codeforces 164 E Compatible Numbers

    主题链接~~> 做题情绪:好题,做拉的比赛的时候想了非常久,想到枚举变幻某一位的 0 为 1 .可是每一个数都这样枚举岂不超时的节奏,当时没想到事实上从大到小枚举一次就 ok 了. 解题思路: ...

  6. Android Studio使用心得 - 简单介绍与环境配置

    FBI Warning:欢迎转载,但请标明出处:http://blog.csdn.net/codezjx/article/details/38544823,未经本人允许请勿用于商业用途.感谢支持! 关 ...

  7. 用XAML做网页!!—开篇

    原文:用XAML做网页!!-开篇 这几日一直没发表新文章,一来是因为事比较多,二来就是我在研究使用XAML挑战传统HTML来做网页,这很可能是在全球的首次尝试,至少我从未找到任何可供参考的相关资料. ...

  8. bootstrap之DumpWindowHierarchy

    DumpWindowHierarchy package io.appium.android.bootstrap.handler; import android.os.Environment; impo ...

  9. C 一个字符串有三段,第一段原样输出,第二段为要输出字符串的长度,第三段为依据第二段长度补齐第一段

    C 一个字符串有三段,第一段原样输出.第二段为要输出字符串的长度,第三段为依据第二段长度补齐第一段 比如:输入abc 11 12.输出abc12121212 #include<stdio.h&g ...

  10. POJ 1287 Networking(最小生成树)

    题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...