This recipe is the most important recipe in this chapter! Let's take a look at a very common case, where we write some function that accepts a string and returns a part of the string between character values passed in the starts and ends arguments:
#include <string>
#include <algorithm>
std::string between_str(const std::string& input, char starts, char ends) {
std::string::const_iterator pos_beg
= std::find(input.begin(), input.end(), starts);
if (pos_beg == input.end()) {
return std::string();
}
++ pos_beg;
std::string::const_iterator pos_end
= std::find(pos_beg, input.end(), ends);
return std::string(pos_beg, pos_end);
}
Do you like this implementation? In my opinion, it is awful. Consider the following call to it:
between_str("Getting expression (between brackets)", &apos...