string/string3.cpp

The following code example is taken from the book
The C++ Standard Library - A Tutorial and Reference, 2nd Edition
by Nicolai M. Josuttis, Addison Wesley Longman, 2012
Copyright © 2012 by Pearson Education, Inc. and Nicolai M. Josuttis


#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <locale>
using namespace std;

int main()
{
    string input;

    // don't skip leading whitespaces
    cin.unsetf (ios::skipws);

    // read all characters while compressing whitespaces
    const locale& loc(cin.getloc());    // locale
    unique_copy(istream_iterator<char>(cin),  // beginning of source
                istream_iterator<char>(),     // end of source
                back_inserter(input),         // destination
                [=] (char c1, char c2) {      // criterion for adj. duplicates
                    return isspace(c1,loc) && isspace(c2,loc);
                });

    // process input
    // - here: write it to the standard output
    cout << input;
}