简体   繁体   中英

Replace for loop with std::find_if and a lambda

Consider this code:

int i{ -1 };
for(i=0; i<size; ++i) if (predicate(i)) break;
bool found{ i < size };

This should be simple to replace with std::find_if, but it needs an iterator on integers in the range of 0 to size-1. (The equivalent of C#'s IEnumerable Enumerable.Range(0, size-1) .)

Since you tagged your question with boost tag, I assume boost::irange along with boost::find_if will do the work:

boost::find_if(boost::irange(0, size - 1), predicate);

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