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 ...
随机推荐
- poj3613(恰经过N条边的最短路)
题目连接:http://poj.org/problem?id=3613 题意:从S 到 T 经过边得个数恰为k的最短路是多少. 分析:01邻接矩阵A的K次方C=A^K,C[i][j]表示i点到j点正好 ...
- GOJ1150(矩阵快速幂)
sum Time Limit: 1000ms Problem Description: 给定a和n,计算a+aa+aaa+aaaa+...+a...a(n个a) 的和. Input: 测试数据有多组, ...
- PHP上传文件超过了最大文件大小限制导致无法上传成功
最近的研究<HeadFirst PHP & MySQL>第一本书5章"使用存储在文件中的数据",难道当一个文件上传应用程序,发生了错误.即,文件不能成功上传.这 ...
- 解决vmware“二进制转换和长模式与此平台兼容.....”问题
问题描述: 启动vmware显现:1.二进制转换和长模式与此平台兼容....字等.: 2.vmware启动一会,系统直接重新启动,这个现象出如今惠普电脑上 问题原因: 出现这种原因一般都是因为系统Vi ...
- 单服务器防护linux iptables脚本
#!/bin/bashiptables -Fiptables -P INPUT DROPiptables -P OUTPUT ACCEPTiptables -P FORWARD DROP/sbin/i ...
- Codeforces 191 C Fools and Roads (树链拆分)
主题链接~~> 做题情绪:做了HDU 5044后就感觉非常easy了. 解题思路: 先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作.经过树链剖分后,就会形成很多链,可是每条边 ...
- oracle database 12c R1 安装文档
INSTALLORACLE DATABASE 12C 完整的安装文档下载地址: http://download.csdn.net/detail/royjj/5665869 OS:ORALCE LINU ...
- arcgis jsapi 调用google地区服务
做地理信息系统(GIS)项目,除了实现功能用户体验度要好之外,最重要的是地图渲染效果更要好.很多时候苦于数据的完整性和对于配图的审美观,程序猿们都很难配出好看的地图效果.基于上述一般直接调用googl ...
- JS CSS 网页 简单 右侧 悬浮
<!--右侧效果--> <script> $().ready(function() { $(".orm").hover(function() { $(thi ...
- WPF命中测试示例(一)——坐标点命中测试
原文:WPF命中测试示例(一)--坐标点命中测试 命中测试也可被称为碰撞测试,在WPF中使用VisualTreeHelper.HitTest()方法实现,该方法用于获取给定的一个坐标点或几何形状内存在 ...