<STL應用> vector & find_if

看到有人問有個名為C的struct如下

code:

struct C
{
int v1;
int v2;
};

應用在vector中式宣告成vector<C> cv;

如果要搜尋內部元素時該怎麼做?? 
一般解法通常是用for()迴圈作線行搜尋,其實這樣滿直覺也滿簡單的。

這裡提供另一種簡易解法.... 
find_if()的第三個參數提供判斷式的傳入,但是很可惜,只能傳入一個參數。 
而且傳入的是*iterator的型別,而非比對樣本值。

例如這個cv裡的物件的v1,v2依序為 
v1 v2 
1 100 
2 52 
3 25 
4 75 
5 84 
6 33

那我們要找v2=75時的物件該如何做? 
1.functor point

code:

bool Cfind75(const C& obj)
{
return obj.v2==75;
}

2.functor

code:

class Cfind75{
public:
bool operator()(const C& obj)
{
return obj.v2==75;
}
};

可是這樣要比較值就被寫死了啊!! 
沒錯....所以這時候template就派上用場了.... 
所以我們改寫

code:

template<int n>
class CComp{
public:
bool operator()(const C& lhs)
{
return (lhs.v2==n);
}
};

如何使用呢??很簡單....

code:

vector<C>::iterator cviter =
find_if(cv.begin(),cv.end(),CComp<75>());

則cviter就是傳回cv中C型別物件的v2值為75的位置。 
-- 
很簡單的小應用,也沒啥大學問。但是對STL不熟的往往都會忽略。

 #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
struct C
{
C():v1(),v2(){}
C(const int& val1,const int& val2):v1(val1),v2(val2){}
C operator()(const int& val1,const int& val2)
{
v1=val1;
v2=val2;
return *this;
}
~C(){}
int v1;
int v2;
};
template<int n>
class CComp{
public:
bool operator()(const C& lhs)
{
return (lhs.v2==n);
}
};
int main(int argc, char *argv[])
{
vector<C> cv;
C val;
cv.push_back(val(,));
cv.push_back(val(,));
cv.push_back(val(,));
cv.push_back(val(,));
cv.push_back(val(,));
cv.push_back(val(,)); vector<C>::iterator cviter =
find_if(cv.begin(),cv.end(),CComp<>());
cout<<cviter->v1<<" "<<cviter->v2<<endl;
cout<<endl; system("PAUSE");
return ;
}

C++中的vector&find_if的更多相关文章

  1. 【转】java.util.vector中的vector的详细用法

    [转]java.util.vector中的vector的详细用法 ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.uti ...

  2. 转:用STL中的vector动态开辟二维数组

    用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int mai ...

  3. STL中的Vector相关用法

    STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int> ...

  4. (转)C++ STL中的vector的内存分配与释放

    C++ STL中的vector的内存分配与释放http://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html 1.vector的内 ...

  5. C++STL中的vector的简单实用

    [原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...

  6. STL中的vector实现邻接表

    /* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include  ...

  7. 数据结构与算法(3)- C++ STL与java se中的vector

    声明:虽然本系列博客与具体的编程语言无关.但是本文作者对c++相对比较熟悉,其次是java,所以难免会有视角上的偏差.举例也大多是和这两门语言相关. 上一篇博客概念性的介绍了vector,我们有了大致 ...

  8. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  9. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

随机推荐

  1. oracle行转列

    wm_concat()函数 --创建表 create table test(id number,name varchar2(20)); --插入数据 insert into test values(1 ...

  2. dblinks

    一.Oracle数据库链Database links的作用 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本 ...

  3. LR开发接口脚本

    char token_id,tenant_id;    web_add_header("Accept","application/json");    web_ ...

  4. 【mysql】source导入多个文件

    在mysql中,可以将表导出为sql文件,比如1.sql, 2.sql等等. 导入一个文件: source /home/somepath/.sql 那么问题来了,如果我想一次导入100个文件呢?总不能 ...

  5. java 标准异常

    一.Throwable这个Java类被用来表示任何可以作为异常被抛出的类.Throwable对象可分为两种类型(从Throwable继承而得到的类型): 1.) Error用来表示编译时和系统错误 2 ...

  6. Notes for building gimp-print

    First try, build gimp-print on ubuntu. 1. Install all dependencies. sudo apt-get install libcups2-de ...

  7. .NetCore Linux环境下安装InfluxDB以及配置设置

    Linux下安装 确定需要安装的版本,我的linux是干净的,所以我需要先安装wget yum -y install wget 下载安装 wget https://dl.influxdata.com/ ...

  8. hdu 5007 水题 (2014西安网赛A题)

    题意:出现Apple.iPod.iPhone.iPad时输出MAI MAI MAI!,出现Sony,输出SONY DAFA IS GOOD! Sample InputApple bananaiPad ...

  9. Atom组件

    Atom组件 最近用Atom写博客比较多,然后发现一个很严重的问题.. 没有一个我想要的上传图片的方式,比如某乎上边就可以直接copy/paste文件,然后进行上传. 然而在Atom上没有找到类似的插 ...

  10. Xcode6.1 模拟器路径

    Xcode 5的iOS模拟器的应用的目录是在~/Library/Application Support/iPhone Simulator/<iOS_Version>/Application ...