Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

max_element in lambda function [closed]

Tags:

c++

lambda

max

I implemented this function but it still give the following error and I can't figure out why..

/usr/include/c++/4.8/bits/stl_algo.h:6325: error: no match for call to '(Farm::killHeaviestAnimalOnFarm()::__lambda0) (Animal*&, Animal*&)' if (__comp(*__result, *__first)) ^

I searched some examples on the internet and I cannot see what's the difference between mine..

My .cpp file had the following killHeaviestAnimalOnFarm

Animal *Farm::killHeaviestAnimalOnFarm()
{
    auto minmax_widths = std::max_element(animals.begin(), animals.end(),
            [] (Animal const& lhs, Animal const& rhs) {
                return lhs.getWeight() < rhs.getWeight();
            });
}
like image 212
Tim Dirks Avatar asked Aug 15 '15 23:08

Tim Dirks


People also ask

What is Max_element?

std::max_element is defined inside the header file and it returns an iterator pointing to the element with the largest value in the range [first, last).

What does Max () with a key do in Python?

max() returns the largest element from an iterable.

Where is the disk space allocated for lambda functions?

Temporary storage with /tmp The Lambda execution environment provides a file system for your code to use at /tmp. This space has a fixed size of 512 MB. The same Lambda execution environment may be reused by multiple Lambda invocations to optimize performance.

Can One Lambda have multiple handlers?

Best practices suggest that one separate the handler from the Lambda's core logic. Not only is it okay to add additional definitions, it can lead to more legible code and reduce waste--e.g. multiple API calls to S3.


1 Answers

If animals is vector<Animal*> then change your lambda function to :

       auto minmax_widths = std::max_element(animals.begin(), animals.end(),
            [] (Animal const * lhs, Animal const * rhs) {
            return lhs->getWeight() < rhs->getWeight();
    });

The alternative is to make animals a vector<Animal> in which case your lambda works as is.

like image 188
Christophe Avatar answered Sep 19 '22 12:09

Christophe