I have come up with this approach but I'm not convinced about its effectiveness. Is there any effective approach?
#include <string>
#include <iostream>
template <typename Container, typename UnaryPredicate>
auto remove_if(Container& c, UnaryPredicate pred)
-> 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 <typename Container, typename T>
auto remove(Container& c, T const& value)
-> decltype(c.begin())
{
return remove_if(c, [&](T const& t) { return t == value; });
}
int main()
{
std::string str = " Text with some spaces ";
remove(str, ' ');
std::cout << str ;
}