For learning purposes and to understand how things work, I'm trying to rewrite this without templates, in the case wstring
:
#include <ctype.h>
template<typename charT>
struct my_equal
{
bool operator()(charT ch1, charT ch2) { return toupper(ch1) == ch2; }
};
template<typename T>
bool contains(const T& str1, const T& str2)
{
typename T::const_iterator it = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(), my_equal<typename T::value_type>());
return (it != str1.end());
}
I'm trying this:
struct my_equal
{
bool operator()(wchar_t ch1, wchar_t ch2) { return toupper(ch1) == ch2; }
};
bool contains(const wstring str1, const wstring str2)
{
wstring::const_iterator it = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(), my_equal());
return (it != str1.end());
}
It works but it's twice or three times slower when benchmarking it. Why?
Is there something wrong in the "translation without templates"?
Also, is it possible to avoid using a struct
but having the my_equal
comparison directly in search(...)
?
You omitted some ampersands. Make it,
bool contains(const wstring &str1, const wstring &str2)
{
wstring::const_iterator it = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(), my_equal());
return (it != str1.end());
}