用谓词使用std :: find [英] using std::find with a predicate

查看:160
本文介绍了用谓词使用std :: find的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 std :: find 函数和一个谓词(不确定是否使用正确的单词)。这里是代码

  #include< iostream> 
#include< vector>
#include< algorithm>
使用namespace std;
class foo {
public:
typedef pair< int,vector< int> >办法;
typedef pair< int,int>指数;
typedef pair< index,vector< way> >条目;
vector<条目>表;

void bar()
{
vector< int> V1;
v1.push_back(1);
v1.push_back(2);

way w = make_pair(1,v1);
vector< way> V2;
v2.push_back(w);

index id = make_pair(10,20);
entry en = make_pair(id,v2);
table.push_back(en);

void insert()
{
index new_id = make_pair(10,20);
if(table.begin(),table.end(),new_id)!= table.end()){
//表中匹配的索引
//然后I将推回一个新的对(方式)
//到条目的第二部分
}
}
};
int main()
{
foo f;
f.bar();
f.insert();
返回0;

code $


正如你所看到的, find() code>应根据每个条目中的第一个元素搜索。现在,它表示 == 不会被重载以比较

解决方案

您想 std :: find_if

  ... $ b $如果(find_if(table.begin(),table.end(),[& new_id](const entry& arg){
return arg.first == new_id;})!= ...)

编辑:如果你没有C ++ 11因此不需要lambda表达式),你必须创建一个自定义函子(函数或函数对象)来比较 entry :: first 和搜索到的索引

  struct index_equal:std :: unary_function< entry,bool> 
{
index_equal(const index& idx):idx_(idx){}
bool operator()(const entry& arg)const {return arg.first == idx_; }
const index& idx_;
};
$ b $ ...
if(find_if(table.begin(),table.end(),index_equal(new_id))!= ...)
index
只是一对 int s,你也可以通过值来捕获它,而不是const引用,以保持代码更清晰,更简洁,但也不重要。


I want to use std::find function along with a predicate (not sure if I use the correct word). Here is the code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class foo {
public:
  typedef pair< int, vector<int> > way;
  typedef pair< int, int > index;
  typedef pair< index, vector<way> > entry;
  vector< entry > table;

  void bar() 
  {
     vector<int> v1;
     v1.push_back(1);
     v1.push_back(2);

     way w = make_pair( 1, v1 );
     vector<way> v2;
     v2.push_back(w);

     index id = make_pair( 10, 20 );
     entry en = make_pair( id, v2 );
     table.push_back( en );
  }
  void insert()
  {
     index new_id = make_pair( 10, 20 );
     if ( find(table.begin(), table.end(), new_id) != table.end() ) {
        // index matched in the table
        // then I will push back a new pair (way)
        // to the second part of the entry
     }
  }
};
int main()
{
  foo f;
  f.bar();
  f.insert();
  return 0; 
}

As you can see, find() should search the table based on the first element in each entry. Right now, it says that == is not overloaded to compare a pair.

解决方案

You want std::find_if:

...
if(find_if(table.begin(), table.end(), [&new_id](const entry &arg) { 
                                           return arg.first == new_id; }) != ...)

EDIT: If you don't have C++11 (and therefore no lambdas), you have to create a custom functor (function or function object) to do the comparison of entry::first with the searched index:

struct index_equal : std::unary_function<entry,bool>
{
    index_equal(const index &idx) : idx_(idx) {}
    bool operator()(const entry &arg) const { return arg.first == idx_; }
    const index &idx_;
};

...
if(find_if(table.begin(), table.end(), index_equal(new_id)) != ...)

EDIT: Since an index is just a pair of ints, you may also just capture it by value than const reference, to keep the code clearer and more concise, but it doesn't really matter either.

这篇关于用谓词使用std :: find的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆