Because strings are often read from user input, they may contain wild formatting and often need to be sanitized. One example of this is strings containing too many whitespace.
In this section, we will implement a slick whitespace filtering algorithm, which removes excess whitespace from strings but leaves single whitespace characters untouched. We call that algorithm remove_multi_whitespace
, and its interface will look very STL-like.
In this section, we will implement the remove_multi_whitespace
algorithm and check out how it works:
- As always, we do some includes first and then declare that we use the
std
namespace by default:
#include <iostream> #include <string> #include <algorithm> using namespace std;
- We implement a new STL-style algorithm called
remove_multi_whitespace
. This algorithm removes clustered occurrences of whitespace, but no single spaces. This means that a string line...