简体   繁体   中英

C++ find highest element in array that isant a particular value

I am using some data that uses some an extremely high value as an error code. I am currently using std max element, is there a way to do this ignoring a particular value?. eg max element that isant a particular number.

So, let's use a custom comparator as part of the call to std::max_element . We'll just make sure that if we see the error code, then we'll make that smaller than all other elements.

auto maxElement = std::max_element(std::begin(container), std::end(container), [](T const & lhs, T const & rhs) -> bool {
    if (rhs == error_code)
         return false;
    if (lhs == error_code)
         return true;
    return lhs < rhs;
}

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