std :: string和不区分大小写的比较 [英] std::string and case insensitive comparison
问题描述
进行不区分大小写的比较最有效的方法是什么?
我正在尝试编写一个通用的String类而且我是坚持使用
不区分大小写的部分:
TCHAR是MultiByte String env(MBCS)中的字符
和wchar_t如果是UNICODE
#if定义(WIN32)||已定义(UNDER_CE)
typedef std :: basic_string< TCHAR,std :: char_traits< TCHAR>,
std :: allocator< TCHAR tstring;
#else
#endif
#endif
class String:public Object
{
私人:
tstring m_str;
public:
String(){}
字符串(LPCTSTR lpsz)
{
m_str = lpsz;
}
字符串(tstring str)
{
m_str = str;
}
//比较
int比较(LPCTSTR psz)const
{
返回m_str.compare(psz);
}
int CompareNoCase(LPCTSTR psz)const
{
???
}
//将字符串转换为小写
String& MakeLower(LPCTSTR psz)
{
std :: transform(m_str.begin(),m_str.end(),m_str.begin(),tolower);
返回* this;
}
}
Hi,
what is the most efficient way of doing a case insensitive comparison ?
I am trying to write a universal String class and I am stuck with the
case insensitive part :
TCHAR is a char in MultiByte String env (MBCS)
and wchar_t if UNICODE
#if defined(WIN32) || defined(UNDER_CE)
typedef std::basic_string<TCHAR, std::char_traits<TCHAR>,
std::allocator<TCHAR tstring;
#else
#endif
#endif
class String : public Object
{
private:
tstring m_str;
public:
String(){}
String(LPCTSTR lpsz)
{
m_str = lpsz;
}
String(tstring str)
{
m_str = str;
}
// Comparison
int Compare( LPCTSTR psz ) const
{
return m_str.compare(psz);
}
int CompareNoCase( LPCTSTR psz ) const
{
???
}
// Convert the string to lowercase
String& MakeLower( LPCTSTR psz )
{
std::transform(m_str.begin(), m_str.end(), m_str.begin(), tolower);
return *this;
}
}
推荐答案
在2007-07-20 00:07,Mosfet写道:
On 2007-07-20 00:07, Mosfet wrote:
什么是最多做一个不区分大小写的比较的有效方法?
我正在尝试编写一个通用的String类,并且我坚持使用
不区分大小写的部分:
Hi,
what is the most efficient way of doing a case insensitive comparison ?
I am trying to write a universal String class and I am stuck with the
case insensitive part :
通常它涉及将两个字符串修改为上限或下限
的情况,然后进行比较。查看toupper()/ tolower()函数来自
< cctype> /ctype.h> ;.
-
Erik Wikstr?m
Usually it involves modifying both strings to either upper- or lower-
case and then compare. Checkout the toupper()/tolower() functions from
<cctype>/ctype.h>.
--
Erik Wikstr?m
" Mosfet" < a ******* @ free.frwrote in message
news:46 *********************** @ news.free.fr ...
"Mosfet" <an*******@free.frwrote in message
news:46***********************@news.free.fr...
做一个不区分大小写的最有效方法是什么比较?
我正在尝试写一个通用的String类,我坚持使用案例
不敏感的部分:
TCHAR是MultiByte字符串env(MBCS)中的字符
和wchar_t如果UNICODE
typedef std :: basic_string< TCHAR,std :: char_traits< TCHAR>,
std :: allocator< TCHAR tstring;
#else
#endif
#endif
class String:public Object
{
私人:
tstring m_str;
public:
String() {}
String(LPCTSTR lpsz)
{
m_str = lpsz;
}
String(tstring str)
{
m_str = str;
}
//比较
int比较(LPCTSTR psz)const
{
返回m_str.compare(psz);
}
int CompareNoCase(LPCTSTR psz)const
{
???
}
//将字符串转换为小写
String& MakeLower(LPCTSTR psz)
{
std :: transform(m_str.begin(),m_str.end(),m_str.begin(),tolower);
返回* this;
}
}
Hi,
what is the most efficient way of doing a case insensitive comparison ?
I am trying to write a universal String class and I am stuck with the case
insensitive part :
TCHAR is a char in MultiByte String env (MBCS)
and wchar_t if UNICODE
#if defined(WIN32) || defined(UNDER_CE)
typedef std::basic_string<TCHAR, std::char_traits<TCHAR>,
std::allocator<TCHAR tstring;
#else
#endif
#endif
class String : public Object
{
private:
tstring m_str;
public:
String(){}
String(LPCTSTR lpsz)
{
m_str = lpsz;
}
String(tstring str)
{
m_str = str;
}
// Comparison
int Compare( LPCTSTR psz ) const
{
return m_str.compare(psz);
}
int CompareNoCase( LPCTSTR psz ) const
{
???
}
// Convert the string to lowercase
String& MakeLower( LPCTSTR psz )
{
std::transform(m_str.begin(), m_str.end(), m_str.begin(), tolower);
return *this;
}
}
这是我用的是什么我不确定它是否是最佳的,但它确实有效。
bool StrLowCompare(std :: string String1,std :: string String2)
{
std :: transform(String1.begin(),String1.end(),String1.begin(),
tolower);
std :: transform(String2.begin(),String2.end(),String2.begin(),
tolower);
返回String1 == String2 ;
}
This is what I use. I''m not sure if it''s optimal, but it works.
bool StrLowCompare( std::string String1, std::string String2 )
{
std::transform(String1.begin(), String1.end(), String1.begin(),
tolower);
std::transform(String2.begin(), String2.end(), String2.begin(),
tolower);
return String1 == String2;
}
Jim Langstonaécrit:
Jim Langston a écrit :
" Mosfet" < a ******* @ free.frwrote in message
news:46 *********************** @ news.free.fr ...
"Mosfet" <an*******@free.frwrote in message
news:46***********************@news.free.fr...
>
什么是进行不区分大小写的比较的最有效方法?
我正在尝试编写一个通用的String类,我坚持使用这个案例
不敏感的部分:
TCHAR是MultiByte String env(MBCS)中的一个字符
和wchar_t如果UNICODE
#if定义(WIN32)||已定义(UNDER_CE)
typedef std :: basic_string< TCHAR,std :: char_traits< TCHAR>,
std :: allocator< TCHAR tstring;
#else
#endif
#endif
class String:public Object
{
private:
tstring m_str;
public:
String(){}
字符串(LPCTSTR lpsz)
{
m_str = lpsz;
}
字符串(tstring str)
{
m_str = str;
}
//比较
int比较(LPCTSTR psz)const
{
返回m_str。比较(psz);
}
int CompareNoCase(LPCTSTR psz)const
{
???
}
//将字符串转换为小写
字符串& MakeLower(LPCTSTR psz)
{std :: transform(m_str.begin(),m_str.end(),m_str.begin(),tolower);
return * this;
}
}
>Hi,
what is the most efficient way of doing a case insensitive comparison ?
I am trying to write a universal String class and I am stuck with the case
insensitive part :
TCHAR is a char in MultiByte String env (MBCS)
and wchar_t if UNICODE
#if defined(WIN32) || defined(UNDER_CE)
typedef std::basic_string<TCHAR, std::char_traits<TCHAR>,
std::allocator<TCHAR tstring;
#else
#endif
#endif
class String : public Object
{
private:
tstring m_str;
public:
String(){}
String(LPCTSTR lpsz)
{
m_str = lpsz;
}
String(tstring str)
{
m_str = str;
}
// Comparison
int Compare( LPCTSTR psz ) const
{
return m_str.compare(psz);
}
int CompareNoCase( LPCTSTR psz ) const
{
???
}
// Convert the string to lowercase
String& MakeLower( LPCTSTR psz )
{
std::transform(m_str.begin(), m_str.end(), m_str.begin(), tolower);
return *this;
}
}
这就是我使用的。我不确定它是否是最佳的,但它确实有效。
bool StrLowCompare(std :: string String1,std :: string String2)
{
std :: transform(String1.begin(),String1.end(),String1.begin(),
tolower);
std :: transform(String2.begin(),String2.end(),String2.begin(),
tolower);
返回String1 == String2 ;
}
This is what I use. I''m not sure if it''s optimal, but it works.
bool StrLowCompare( std::string String1, std::string String2 )
{
std::transform(String1.begin(), String1.end(), String1.begin(),
tolower);
std::transform(String2.begin(), String2.end(), String2.begin(),
tolower);
return String1 == String2;
}
Ir看起来效率不高......
Ir doesn''t seem very efficient...
这篇关于std :: string和不区分大小写的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!