有人可以告诉我如何使用 Remove_If 进行一些不区分大小写的比较吗?
目前我这样做:
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;
}
我想用 remove_if 来做这个。或者至少比这更好的方法。它看起来很丑,我决定优化代码,所以我想出了:
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?
}
有任何想法吗?