1

I have the following hand-written loop to process parenthesized expressions:

while (!punctuators.empty() && punctuators.back().kind != '(')
{
    output.push_back(punctuators.back());
    punctuators.pop_back();
}
if (punctuators.empty())
    throw invalid_expression(") without matching matching (");
punctuators.pop_back();

(output and punctuators are both of type std::vector<token>, where token is a very simple struct that consists of a char kind and an unsigned value data member.)

I was wondering if switching from hand-written loops to algorithms would improve readability:

auto p = std::find_if(punctuators.rbegin(), punctuators.rend(),
                      [](token t){ return t.kind == '('; });
if (p == punctuators.rend())
    throw invalid_expression(") without matching matching (");
output.insert(output.end(), punctuators.rbegin(), p);
punctuators.erase(std::prev(p.base()), punctuators.end());

But somehow I feel that this code is a lot less readable, maybe due to the usage of reverse iterators, and especially the conversion to normal iterators. Is there a better solution? Would you agree that the hand-written loop is more readable, or am I simply not seeing the light yet when it comes to algorithms?

1 Answer 1

3

You just need the right algorithm.

(untested code)

boost::algorithm::copy_while ( 
    punctuators.rbegin (), punctuators.rend (), 
    std::back_inserter ( output ),
    [](token t){ return t.kind != '('; });

If you're not using boost, you can write copy_while yourself fairly easily.

There's nothing magical about algorithms; you should get in the habit of writing your own.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.