简体   繁体   English

使用bind2nd和functor在vector <Message *>上找到find_if

[英]find_if on vector<Message*> with bind2nd and functor

I have a vector like this : 我有这样的矢量:

typedef vector<Message*> OmciMessages;
OmciMessages omciResponses;

And I have a functor like following which I suspect is not correct : 我有一个像下面这样的仿函数我怀疑是不正确的:

class isMatching{
public:
    bool operator()(const Message* responseMsg,unsigned short transactionid){       
        return responseMsg->getTransactionId()==transactionid;
    }
};

then I call find_if and want to use the functor to find something in the vector but only for a specific transactionid that is inside my class : 然后我调用find_if并想使用仿函数在向量中找到一些内容,但仅限于我的类中的特定transactionid:

OmciMessages::iterator responseit 
    = find_if(omciResponses.begin(),
              omciResponses.end(),
              bind2nd(isMatching(),transactionid));

The compiler does not like it and spawns quite a lot of error messages that are hard to interpret typically for template classes. 编译器不喜欢它并产生了很多错误消息,这些消息通常很难解释为模板类。

/repo/stuyckp/ngpon2WW/tools/cm4/tools/GNU/src/gcc/i686-pc-linux-gnu/bin/../lib/gcc/i686-pc-linux-gnu/3.4.6/../../../../../include/c++/3.4.6/bits/stl_function.h:429: error: no type named `first_argument_type' in `class isMatching'

It worked if I did it like this without a function object : 如果我这样做没有函数对象,它是有效的:

static bool isMatching(Message* responseMsg){
    return responseMsg->getTransactionId()==transactionid;
}

transactionid = 5; //global variable being used. yuck, don't like it
find_if(omciResponses.begin(),
        omciResponses.end(),
        isMatching);

but then I needed a global variable transaction id that was set beforehand which I think is not that good a design. 但后来我需要一个预先设置的全局变量事务id,我认为这不是一个好的设计。 So how is this supposed to be done with the bind2nd approach ? 那么应该如何使用bind2nd方法呢?

bind2nd needs your functor class to be adaptable, ie provide certain typedefs, including first_argument_type , second_argument_type , and result_type . bind2nd需要你的functor类适应,即提供某些typedef,包括first_argument_typesecond_argument_typeresult_type You can define them by yourself in the functor classes, or inherit from std::binary_function to make it easy: 您可以在functor类中自己定义它们,或者从std :: binary_function继承以使其变得简单:

class isMatching : public std::binary_function<const Message*, unsigned short, bool> {
public:
    bool operator()(const Message* responseMsg, unsigned short transactionid) const {       
        return responseMsg->getTransactionId() == transactionid;
    }
};

Given the version of your compiler, I assume that C++11 is not an option (otherwise the answer is simple: use a lambda). 鉴于您的编译器的版本,我假设C ++ 11不是一个选项(否则答案很简单:使用lambda)。

The old function object binders (deprecated in C++11, removed in C++17) require a bunch of nested typedefs. 旧的函数对象绑定器(在C ++ 11中已弃用,在C ++ 17中删除)需要一堆嵌套的typedef。 You need to define result_type , first_argument_type , and second_argument_type as members of isMatching , either directly or via the binary_function helper (which is also deprecated in C++11 and removed in C++17), to use it with bind2nd . 需要定义result_typefirst_argument_type ,和second_argument_type作为成员isMatching ,无论是直接或经由binary_function助手(也不赞成使用C ++ 11并除去在C ++ 17),与使用它bind2nd

You can also use a plain function taking two arguments, adapt it with ptr_fun (again, deprecated in C++11 and removed in C++17), and then bind with bind2nd . 您还可以使用带有两个参数的普通函数,使用ptr_fun (再次,在C ++ 11中弃用并在C ++ 17中删除),然后使用bind2nd进行绑定。

Or just use a stateful functor, as shown in Piotr's answer. 或者只使用有状态仿函数,如Piotr的回答所示。

struct isMatching
{
    unsigned short transactionid;

    explicit isMatching(unsigned short transactionid) : transactionid(transactionid) {}

    bool operator()(const Message* responseMsg) const
    {       
        return responseMsg->getTransactionId() == transactionid;
    }
};

std::find_if(omciResponses.begin()
           , omciResponses.end()
           , isMatching(0));
//                      ^ transactionId to be searched for

DEMO DEMO

Note that if you urge to use std::bind2nd , or the function object would be stateless, then you don't need a function object as a separate class at all: 请注意,如果您想使用std::bind2nd ,或者函数对象是无状态的,那么您根本不需要函数对象作为单独的类:

bool isMatching(const Message* responseMsg, unsigned short transactionid)
{
    return responseMsg->getTransactionId() == transactionid;
}

std::find_if(omciResponses.begin()
           , omciResponses.end()
           , std::bind2nd(std::ptr_fun(&isMatching), 0));
//                        ~~~~~~~~~~~^               ^ transactionId to be searched for
//      transform to a function object

DEMO 2 演示2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM