3 ms·
Um... maybe it's verbose because you're trying to be too clever? :-P How about auto it = channel_widgets.begin(); while (it != channel_widgets.end())
by zvrba 14y ago
Um... maybe it's verbose because you're trying to be too clever? :-P How about
auto it = channel_widgets.begin();
while (it != channel_widgets.end())
if (it->getType() == type) goto found;
// error
found: // whatever
I kinda hate myself every time I write such for-loop, but it's still more succint than any variant of find_if. Generally, I use "trivial" STL algorithms (find, find_if, for_each, ...) only if I can reuse the functor several times. Otherwise it's not worth the hassle.
PS: you can do it also without goto... use "break" after if, and after while you write
if (it == channel_widgets.end())
whatever; // not_found
- simmons 14y agoA similar approach occurred to me as I was writing that example. (Using a range-based for loop, break on found.) I guess I'm mostly scratching my head trying to figure out find_if's place in the universe.