How do I check if ranges:: algorithms like find_if returned a value?

元气小坏坏 提交于 2021-02-10 06:43:49

问题


For example, if I want to find the smallest element of a collection, but only the smallest even element, I'd like to call ranges::min_element with a filtered range like so:

using ranges::views::filter;
using ranges::min_element;
std::vector<int> vals{1,2,3,4,5};
auto minVal = min_element(vals | filter([](int next){return next % 2 == 0;}));

How do I check if the returned range is empty, and if not, access the value?

The same applies to other range algorithms like ranges::find, ranges::find_if, etc.


回答1:


Unfortunately you cannot pass a temporary range into ranges algorithms.

This is because they will return a ranges iterator (possibly ranges::end(rng)) upon completion. If you pass a temporary, the returned iterator would be invalid; referencing a temporary. The ranges library detects this and returns a dummy sentinel, ranges::dangling instead of an iterator.

The proper way to do this is to first compose your filtered range then call the algorithm.

std::vector<int> vals {1, 2, 3, 4, 5};
auto even_vals = vals | filter([](int val){return val % 2 == 0;});
auto minVal = min_element(even_vals);
if (minVal == even_vals.end())
    std::cout << "no values\n";
else
   std::cout << *minVal; // prints "2"

Demo



来源:https://stackoverflow.com/questions/58400325/how-do-i-check-if-ranges-algorithms-like-find-if-returned-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!