In general, std::string is a regular container. same as regular std::vector .
If you don't want remove / remove_if and the soul needs lambdas, you can
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { std::string some_string = "Intel<R> Core<TM>2 Duo CPU P8400 @ 2.26GHz"; auto end = std::unique(some_string.begin(), some_string.end(), [](char l, char r){ return std::isspace(l) && std::isspace(r) && l == r; }); std::cout << std::string(some_string.begin(), end) << '\n'; return 0; }
UPD
Some suggest rewriting condition
std::isspace(l) && std::isspace(r) && l == r
as
std::isspace(l) && l == r
I would still rewrite it as
std::isspace(l) && std::isspace(r)
Why? The question sounds "remove extra spaces and tabs." And if the line is this (the underscore is a space, and \t is a tab)
a\t\t__\t_\tb
it would probably be correct to shorten it to a_b , and not a\t_\t_\tb .
But if you look at what exactly std::isspace , you will find out that the newline ( \n ) is also considered. Therefore, if the line is a multi-line view abc_\n_def , then it will be reduced to abc_def , which may not be exactly what you want. By the way, this option is closer to the regular version in the answer @GreenDragon, since \s works like isspace .