Listing 7
bool first_alpha(const std::string & s) {
return !s.empty() && isalpha(s[0]);
}
int get_len(const std::string &s) { return s.length(); }
void to_upper(std::string & s) {
rng::transform(s, s.begin(), toupper);
}
int main() {
std::vector<std::string> words;
std::ifstream in("readme.txt");
std::string str;
while ( in >> str) words.push_back(str);
// compute the length of all words starting with an alphanumeric char
int len = rng::accumulate(
transformed_f(filtered(words,first_alpha),get_len), 0);
// shows the words in sorted order,
leaving original 'words' array unchanged!
rng::copy( resorted(words),
std::ostream_iterator<std::string>(std::cout," "));
// copy the words into another array, in reversed order
std::vector<std::string> words_rev;
rng::copy( reversed(words), std::back_inserter(words_rev) );
// convert strings that begin with an alphanumeric char to uppercase
rng::for_each( filtered(words_rev,first_alpha), to_upper);
}