Skip to content

Instantly share code, notes, and snippets.

@bolry
Created December 28, 2019 23:58
Show Gist options
  • Save bolry/eb5bc65e1b5a14f25422e81880a0aced to your computer and use it in GitHub Desktop.
Save bolry/eb5bc65e1b5a14f25422e81880a0aced to your computer and use it in GitHub Desktop.
reverse_if and reverseOnlyLetters
#include <algorithm>
#include <boost/iterator/filter_iterator.hpp>
template <class Iter, class Predicate>
void reverse_if(Iter first, Iter last, Predicate pred) {
std::reverse(boost::make_filter_iterator(pred, first, last),
boost::make_filter_iterator(pred, last, last));
}
#include <locale>
void reverseOnlyLetters(std::string &s) {
reverse_if(s.begin(), s.end(),
[](char c) { return std::isalpha(c, std::locale{}); });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment