Reputation: 23485
Can someone show me how to use Remove_If with some case-insensitive comparison?
Currently I do it like:
template<typename T>
struct is_literal
{
enum{value = false};
};
template<>
struct is_literal<char>
{
enum{value = true};
};
template<>
struct is_literal<char*>
{
enum{value = true};
};
template<>
struct is_literal<const char*>
{
enum{value = true};
};
template<typename Char, typename Traits, typename Alloc>
struct is_literal<std::basic_string<Char, Traits, Alloc>>
{
enum{value = true};
};
template<typename T>
template<typename U>
CustomType<T>& CustomType<T>::Delete(U ValueToDelete, bool All, typename std::enable_if<is_literal<U>::value, bool>::type CaseSensitive)
{
for (std::size_t I = 0; I < TypeData.size(); ++I)
{
if (CaseSensitive ? std::string(TypeData[I]) == std::string(ValueToDelete) : std::string(ToLowerCase(TypeData[I])) == std::string(ToLowerCase(ValueToDelete)))
{
TypeData.erase(TypeData.begin() + I);
if (!All)
break;
--I;
}
}
return *this;
}
I want to do this with remove_if some how.. OR at least a better way of doing it than this.. It just looks ugly is all and I decided to optimize the code so I came up with:
if (CaseSensitive)
{
if (All)
{
TypeData.erase(std::remove(TypeData.begin(), TypeData.end(), ValueToDelete), TypeData.end());
}
else
{
TypeData.erase(std::find(TypeData.begin(), TypeData.end(), ValueToDelete));
}
}
else
{
//Do I have to forloop and lowercase everything like before then compare and remove? OR can I do it just like above using std::remove or std::find with some kind of predicate for doing it?
}
Any ideas?
Upvotes: 2
Views: 315
Reputation: 33457
You could use a struct that holds state:
struct StringEqualityPredicate {
std::string str;
bool caseSensitive;
StringEqualityPredicate(const std::string& str, bool caseSensitive = true) : str(str), caseSensitive(caseSensitive)
{ }
bool operator(const std::string& str)
{
if (caseSensitive) {
//...
} else {
//...
}
}
};
std::erase(std::remove_if(v.begin(), v.end(), StringEqualityPredicate(targetVal, caseSensitive)), v.end());
Since you're using C++11, there's likely a more compact, cleaner way to do it (a lambda for example), but my C++11 knowledge is basically non-existent.... :).
This hasn't really accomplished much though other than moving your if block into a struct rather than inline in your method, but if you find yourself doing this in more than one place (or you just want to abstract it a bit), this could be useful.
Upvotes: 2
Reputation: 179552
Well, since remove_if
takes a predicate argument, this should be pretty easy with a lambda:
std::remove_if(TypeData.begin(), TypeData.end(),
[&ValueToDelete](U &val){ return ToLowerCase(val) == ToLowerCase(ValueToDelete); });
Upvotes: 4