P
P
Pavel2016-02-07 20:43:17
C++ / C#
Pavel, 2016-02-07 20:43:17

How to specify a lambda function as a parameter in a template function?

There is a task where you need to decompose one container into 2 others according to a condition that will be set by a lambda function.
How can I specify in the template as a parameter that this is a lambda function?

template<typename inputType,typename leftContainer,typename rightContainer>
void OddEven(inputType input,leftContainer left,rightContainer right,лямбда функция){
    for(auto key:input){
        if(лямбда функция(key)){
            left.insert(left.end(),key);
        }else{
            right.insert(right.end(),key);
        }
    }
}

{
    //Например:
    std::vector<int> v{ 1,2,3,4,5 };
    std::list<int> l; //сюда четные
    std::deque<int> d; //а сюда нечетные
    OddEven(v, l, d, [](const auto &val){val>0?true:false;});
    
    asm("nop");
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2016-02-07
@rusbaron

As in the case of a regular function:

template <class F>
void foo(F f) {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question