A c++0x - solution using a lambda instead of a regular function object. Compare to Cubbi's solution.
#include
#include
int main()
{
std::string str = "I Like StackOverflow a lot";
str.erase(std::unique(str.begin(), str.end(),
[](char a, char b) { return a == ' ' && b == ' '; } ), str.end() );
}