简体   繁体   中英

Replacing C++ while loop with std/boost algorithm

I have a while-loop that I'd like to replace with an std/boost algorithm (largely for my learning, along the line of Sean Parent's no-raw-loops maxim ).

std::queue q;
while (! q.empty()) {
    auto front = q.front();
    q.pop();
    do_stuff_with_front();
    potentially_insert_more_into_q();
}

As you can see, I conditionally enqueue more elements within the loop; think maze traversal. What std/boost algorithm may be suitable for this?

I can't think of any algorithm or alternate approach that will yield code more clear than the while loop you've already written. All standard algorithms I can remember work on a fixed range, and since your code is adding elements that's not consistent with standard algorithm interfaces.

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