STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. STL算法的一个版本采用缺省的运算行为,该算法的另一个版本提供额外参数,接收外界传入的一个仿函数(functor),以便采用其他策略。可以采用其他策略的算法通常是以_if作为尾词,例如find_if(), replace_if().
与此类似的,质变算法(改变操作区间内元素的值)分为in-place(就地计算)和copy(将对象内容拷贝一份副本,在副本上运算完毕后返回)版,copy版总是以_copy作为函数名称尾词。
小例子:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <boost/format.hpp>
#include <boost/cstdint.hpp> using namespace std;
using namespace boost; struct find_func{
find_func(unsigned int var1)
: var(var1) {
} bool operator()(const std::map<boost::uint64_t,boost::uint64_t>::value_type &pair) {
int var1 = pair.second;
return (var == var1);
} unsigned int var;
}; static std::map<boost::uint64_t,boost::uint64_t> test_map; int main()
{
std::cout<<"STD::FIND--------------------------------"<<std::endl;
std::vector<int> v;
for (int i = ; i < ; ++i)
v.push_back(i); std::vector<int>::iterator iter = std::find(v.begin(), v.end(), ); 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; std::cout<<"STD::FIND_IF--------------------------------"<<std::endl;
test_map[] = ;
test_map[] = ;
test_map[] = ;
std::map<boost::uint64_t,boost::uint64_t>::iterator it = test_map.end();
it = std::find_if(test_map.begin(), test_map.end(), find_func());
if (it == test_map.end()) {
cout<<"Can not find the int variable"<<endl;
} else {
std::cout << "the index of value " << (*it).first << " is "
<< std::distance(test_map.begin(), it) << std::endl;
} return ;
}
//Output:
STD::FIND---------------------------
The index of value 3 is 3
STD::FIND_IF------------------------
the index of value 2000 is 1
STL的std::find和std::find_if的更多相关文章
- STL 1–迭代器std::begin()和std::end()使用
迭代器是一个行为类似于指针的模板类对象.只需要迭代器iter指向一个有效对象,就可以通过使用*iter解引用的方式来获取一个对象的引用.通常会使用一对迭代器来定义一段元素,可以是任意支持迭代器对象的元 ...
- iOS出现 Undefined symbols for architecture armv7 std::basic_string<char, std::char_traits<char>
Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ...
- 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)
从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...
- C++ std::unordered_map使用std::string和char *作key对比
最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...
- 关于C++11中的std::move和std::forward
std::move是一个用于提示优化的函数,过去的c++98中,由于无法将作为右值的临时变量从左值当中区别出来,所以程序运行时有大量临时变量白白的创建后又立刻销毁,其中又尤其是返回字符串std::st ...
- C++11 std::unique_lock与std::lock_guard区别及多线程应用实例
C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...
- C++ 全排列函数 std::next_permutation与std::prev_permutation
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- Error 2 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'
Error 2 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree ...
- C++0x,std::move和std::forward解析
1.std::move 1.1std::move是如何定义的 template<typename _Tp> constexpr typename std::remove_reference ...
随机推荐
- Python 基础之一变量和赋值
变量:程序在运行的时候会用到很多临时存储数据,这个时候就用到了变量,临时数据的名字. Python中变量不需要声明,直接可以使用,变量的数据类型由赋值确定. >>> name=&qu ...
- VS2015墙内创建ionic2 【利用nrm更换源,完美!】
STEP 1 设置cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 一句话建立cnpm STEP 2 安装nr ...
- 如何使用dos命令打开当前用户、当前日期、当前时间以及当前用户加当前时间?
1.dos命令安装mysqld --stall.启动net start mysql.进入MySQL数据库mysql -uroot -p后,输入select user();当前用户 select cur ...
- Win7安装MySQL-5.7.16过程
1.在C盘新建MYSQL文件夹:2.将mysql-5.7.16-winx64拷贝到C:\MYSQL文件夹下,更名为mysql-5.7.16:3.在mysql-5.7.16目录下,建my.ini文件,内 ...
- windows系统路径环境变量
当前系统盘符%systemdrive%或%HOMEDRIVE%C:\ 当前系统目录%systemroot%或%Windir%C:\WINDOWS 当前用户文件夹%UserProfile%或%HOMEP ...
- 执行git push出现"Everything up-to-date"
在github上git clone一个项目,在里面创建一个目录,然后git push的时候,出现报错"Everything up-to-date" 原因:1)没有git add . ...
- 让Mono 4在Raspberry Pi上飞
最近公司有项目想要在树莓派上做,代替原来的工控机(我们是把工控主机当作小的主机用,一台小的工控主机最少也要600左右,而树莓派只要200多).于是,公司买了一个Raspberry Pi B+和一个Ra ...
- Disque:Redis之父新开源的分布式内存作业队列
Disque是Redis之父Salvatore Sanfilippo新开源的一个分布式内存消息代理.它适应于"Redis作为作业队列"的场景,但采用了一种专用.独立.可扩展且具有容 ...
- 《深入理解Java虚拟机》类文件结构
上节学习回顾 在上一节当中,主要以自己的工作环境简单地介绍了一下自身的一些调优或者说是故障处理经验.所谓百变不离其宗,这个宗就是我们解决问题的思路了. 本节学习重点 在前面几章,我们宏观地了解了虚拟机 ...
- iOS开发系列--数据存取
概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列-Objective-C之Foundation框架的文章中提到归档.plist文件存储, ...