简体   繁体   中英

How to use functor which returns two value with standard library functions?

I want to solve the following problem: I have a system of equations

   ax+by=c

   dx+ey=f

I have read in a tutorial that we can use a functor like the following

bool isIdd(int i){
 return ((i%2)==1)
}

and than we can use this functor with the find_if function.

I want to ask if it is possible two return two values from a functor (for example a tuple) and can we use it with find_if STL function?

is possible to return two values from a functor (for example a tuple)

yes,it is,but you need also proper

operator bool()

As I understood, you want something like this:

#include <utility>
#include <algorithm>
#include <iterator>
#include <functional>

struct Pair{

    bool b;
    uint32_t data;

    operator bool()const{
        return this->b;
    }

    const Pair & isIdd(uint32_t p){
        if (p == this-> data) this->b = true;
     return *this ;
    }

    Pair(bool b , uint32_t data){
        this-> b = b;
        this->data = data;
    }

};

int main(){
    using namespace std::placeholders;
    Pair p(0,0);
    std::vector<uint32_t> vct {2};
    auto it = vct.begin ();
    auto b = std::bind(&Pair::isIdd,_1,_2);
    std::find_if(vct.begin (),vct.end (),b(p,*it++ ));
}

But what is here is not relevant to the task set as the solution of the equation. If this fits to you and you really want to use find_if as this,turn it into template and somehow store found data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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