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 ...
随机推荐
- 用 C++ 标准模板库(STL)的 vector 实现二叉搜索树(BST)
本文由 Justme0翻译自 Code Project 转载请参见文章末尾处的要求. 介绍 众所周知,要建一棵树,我们需要关注它的内存分配与释放.为了避开这个问题,我打算用C++ STL(vector ...
- C# 开发Chrome内核浏览器(WebKit.net)
原文地址:http://www.cnblogs.com/linyijia/p/4045333.html
- MySQLdb 连接Mysql 数据库出错解决
#coding=utf-8 import MySQLdb if __name__ == "__main__": db = MySQLdb.connect(host=<sp ...
- Android模拟器设置竖屏
使用Android模拟器測试自己开发的程序时,有时候会发现屏幕为横屏显示,查看效果非常不方便. 这里记录了一种禁止横屏的方法. 在文件 Mainfest.xml 中,在须要禁止横屏的 activit ...
- httpcomponents-client-4.3.x DOC
Chapter 1. Fundamentals Prev Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...
- vi 按了ctrl+s之后
再windows不管是写程序.还是用Word写文件.已经习惯了按ctrl+s 保存代码. 在用vi的时候.常常无意中按了ctrl+s,结果就是如同终端死掉了一样. 这是由于ctrl+s 终止屏幕输出( ...
- java环境变量设置--编写一年java,竟不会配变量了
java环境变量设置 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Program Files\Ja ...
- app服务器
http://heipark.iteye.com/blog/1847421http://heipark.iteye.com/blog/1847421http://wenku.baidu.com/vie ...
- Javascript 设计模式 辛格尔顿
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/30490955 我一直很喜欢Js,,,今天写JsSingleton模式来实现,以及 ...
- C++第11周(春)项目1 - 存储班长信息的学生类
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...