Effective way to remove white spaces from string I have come up with this approach but I'm not convinced about its effectiveness. Is there any effective approach?

#include &lt;string&gt;
#include &lt;iostream&gt;

template &lt;typename Container,  typename UnaryPredicate&gt;
auto remove_if(Container&amp; c, UnaryPredicate pred)
    -&gt; decltype(c.begin())
{
    auto it = std::begin(c);
    while (it != std::end(c))
    {
        if(pred(*it))
        {
            it = c.erase(it);
        }
        else
        {
            ++it;
        }
    }
    return it;
}

template &lt;typename Container, typename T&gt;
auto remove(Container&amp; c,  T const&amp; value)
    -&gt; decltype(c.begin())
{
    return remove_if(c,  [&amp;](T const&amp; t) { return t == value; });
}

int main()
{
    std::string str = " Text with some spaces ";
    remove(str, ' ');
    std::cout &lt;&lt; str ;
}
General Comment
Stop trying to re-invent the wheel.
Or at least go look at a more interesting wheel that has not been optimized to the point were you can't do better.
Code Review
You are calling std::end() every iteration of the loop. Its not a very ineffecient call. But its still doing work they you probably don't need to in most containers.
    while (it != std::end(c))

The standard erase-remove idiom does not need to do this because it works out what it needs to remove first. Thus saving this cost (even if it is very small).
This does not work on all container types (in pre C++11):
             it = c.erase(it);

On containers like std::vector the erase(it) is very expensive. As you have to move all the elements down by one place each time you call erase. This is why the erase-remove idiom does it the other way. It moves elements down as many places as they need to go in a single shot without having to do multiple copies (one move per element).