c++ stl algorithm: std::find, std::find_if
std::find:
查找容器元素, find仅仅能查找容器元素为<基本数据类型>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- int main()
- {
- std::vector<int> v;
- for (int i = 0; i < 10; ++i)
- v.push_back(i);
- std::vector<int>::iterator iter = std::find(v.begin(), v.end(), 3);
- if (iter == v.end())
- std::cout << "can not find value 3 in v" << std::endl;
- else
- std::cout << "the index of value " << (*iter) << " is " << std::distance(v.begin(), iter) << std::endl;
- return 0;
- }
std::find_if
按条件查找容器元素, 容器类型为<类>时, 无法使用find来查找, 所以要使用find_if来查找
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <functional>
- struct Point
- {
- int x;
- int y;
- };
- struct PointFindByCoord : public std::binary_function<Point, Point, bool>
- {
- bool operator () (const Point &obj1, const Point &obj2) const
- {
- return obj1.x == obj2.x && obj1.y == obj2.y;
- }
- };
- int main()
- {
- std::vector<Point> v;
- for (int i = 0; i < 5; ++i)
- {
- for (int j = 0; j < 5; ++j)
- {
- Point pt;
- pt.x = i;
- pt.y = j;
- v.push_back(pt);
- }
- }
- Point needFind;
- needFind.x = 4;
- needFind.y = 3;
- std::vector<Point>::iterator iter = std::find_if(v.begin(), v.end(), std::bind2nd(PointFindByCoord(), needFind));
- if (iter == v.end())
- {
- // 未找到
- }
- else
- std::cout << "the index of value Point(" << (*iter).x << ", " << (*iter).y
- << ") is " << std::distance(v.begin(), iter) << std::endl;
- return 0;
- }
c++ stl algorithm: std::find, std::find_if的更多相关文章
- c++ stl algorithm: std::fill, std::fill_n
std::fill 在[first, last)范围内填充值 #include <iostream> #include <vector> #include <algori ...
- 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 ...
- STL algorithm 头文件下的常用函数
algorithm 头文件下的常用函数 1. max(), min()和abs() //max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须时两个(可以是浮点数) //返回3 ...
- STL algorithm算法merge(34)
merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...
- STL algorithm算法is_permutation(27)
is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- 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++> 给出排序好的 ...
- C++中std::fill/std::fill_n的使用
There is a critical difference between std::fill and memset that is very important to understand. st ...
- std:: lower_bound std:: upper_bound
std:: lower_bound 该函数返回范围内第一个不小于(大于或等于)指定val的值.如果序列中的值都小于val,则返回last.序列应该已经有序! eg: #include <iost ...
随机推荐
- Java程序猿学习当中各个阶段的建议
回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议 引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的 ...
- zoj3471(状压dp)
题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题意:不超过10种气体,两两之间相互碰撞可以产生一定的能量,如 ...
- HDOJ 2736 Surprising Strings
Surprising Strings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- [ACM] hdu 4418 Time travel (高斯消元求期望)
Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...
- Codeforces 164 E Compatible Numbers
主题链接~~> 做题情绪:好题,做拉的比赛的时候想了非常久,想到枚举变幻某一位的 0 为 1 .可是每一个数都这样枚举岂不超时的节奏,当时没想到事实上从大到小枚举一次就 ok 了. 解题思路: ...
- Android Studio使用心得 - 简单介绍与环境配置
FBI Warning:欢迎转载,但请标明出处:http://blog.csdn.net/codezjx/article/details/38544823,未经本人允许请勿用于商业用途.感谢支持! 关 ...
- 用XAML做网页!!—开篇
原文:用XAML做网页!!-开篇 这几日一直没发表新文章,一来是因为事比较多,二来就是我在研究使用XAML挑战传统HTML来做网页,这很可能是在全球的首次尝试,至少我从未找到任何可供参考的相关资料. ...
- bootstrap之DumpWindowHierarchy
DumpWindowHierarchy package io.appium.android.bootstrap.handler; import android.os.Environment; impo ...
- C 一个字符串有三段,第一段原样输出,第二段为要输出字符串的长度,第三段为依据第二段长度补齐第一段
C 一个字符串有三段,第一段原样输出.第二段为要输出字符串的长度,第三段为依据第二段长度补齐第一段 比如:输入abc 11 12.输出abc12121212 #include<stdio.h&g ...
- POJ 1287 Networking(最小生成树)
题意 给你n个点 m条边 求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...