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的更多相关文章

  1. STL 1–迭代器std::begin()和std::end()使用

    迭代器是一个行为类似于指针的模板类对象.只需要迭代器iter指向一个有效对象,就可以通过使用*iter解引用的方式来获取一个对象的引用.通常会使用一对迭代器来定义一段元素,可以是任意支持迭代器对象的元 ...

  2. 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 ...

  3. 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)

    从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...

  4. C++ std::unordered_map使用std::string和char *作key对比

    最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...

  5. 关于C++11中的std::move和std::forward

    std::move是一个用于提示优化的函数,过去的c++98中,由于无法将作为右值的临时变量从左值当中区别出来,所以程序运行时有大量临时变量白白的创建后又立刻销毁,其中又尤其是返回字符串std::st ...

  6. C++11 std::unique_lock与std::lock_guard区别及多线程应用实例

    C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...

  7. C++ 全排列函数 std::next_permutation与std::prev_permutation

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  8. 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 ...

  9. C++0x,std::move和std::forward解析

    1.std::move 1.1std::move是如何定义的 template<typename _Tp> constexpr typename std::remove_reference ...

随机推荐

  1. Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7

    SOLUTION VERIFIED September 13 2016 KB1248793 Environment Red Hat Enterprise Linux 7 NetworkManager ...

  2. Android 指纹认证

    安卓指纹认证使用智能手机触摸传感器对用户进行身份验证.Android Marshmallow(棉花糖)提供了一套API,使用户很容易使用触摸传感器.在Android Marshmallow之前访问触摸 ...

  3. [译] 在Web API 2 中实现带JSON的Patch请求

    原文链接:The Patch Verb in Web API 2 with JSON 我想在.NET4.6 Web API 2 项目中使用Patch更新一个大对象中的某个字断,这才意识到我以前都没有用 ...

  4. 我认为JS还可以改进的点

    曾经我一度在寻找JS的替代语言,尝试过CoffeeScript/TypeScript/Dart(都是浅尝).不为什么原因,而是当你写的越多的JS,越觉得JS很多时候显得很操蛋.好在ES2015和Bab ...

  5. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  6. JavaScript继承的模拟实现

    我们都知道,在JavaScript中只能模拟实现OO中的"类",也就意味着,在JavaScript中没有类的继承.我们也只能通过在原对象里添加或改写属性来模拟实现. 先定义一个父类 ...

  7. [MongoDB] 32Bit构建上文件大小限制问题

    一. 问题概述 今天看看爬虫抓取的数据,发现数据无法插入,首先想到的就是32Bit构建的文件大小限制问题,检查一下还真的是.本文把整个检查问题,解决问题的过程记录下来. 问题:     "s ...

  8. MySQL基础之索引

    这段时间看了好多东西却没有总结,今天在这里写一写 关于索引 索引是一种提高查询效率的方法,它是B+树的结构,从根到中间节点在到叶子节点,无需遍历全部就可以查到所需的东西 关于索引的创建 一般有俩种方式 ...

  9. PHP安全之Web攻击

    一.SQL注入攻击(SQL Injection) 攻击者把SQL命令插入到Web表单的输入域或页面请求的字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响)动态 ...

  10. PHP 面向对象编程和设计模式 (4/5) - 异常的定义、扩展及捕获

    PHP高级程序设计 学习笔记 2014.06.12 异常经常被用来处理一些在程序正常执行中遇到的各种类型的错误.比如做数据库链接时,你就要处理数据库连接失败的情况.使用异常可以提高我们程序的容错特性, ...