如何比较 std::wstring 和 std::wstring字符*?
我所拥有的:
- [wstr1] 1 std::wstring 值(例如“(GMT+04:30) Kabul”)
- [str2] 1 char* 填充相同的值data as std::wstring (具有相同的文本)
我只是想比较这两个值 - 但以下代码似乎不起作用。 KMP。
这是从 char* 到 std::wstring 的转换器,
std::wstring TimeZone::TimeZone::char2ws(const char* src)
{
/* Convert to a wchar_t*
* http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx
*/
// Convert to a wchar_t*
size_t origsize = strlen(src) + 1;
const size_t newsize = 512;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, src, _TRUNCATE);
return wcstring;
}
之后我做了简单的比较,
// wstr1 is std::wstring, with "(GMT+04:30) Kabul" string
// wstr2 = char2ws(str2),
return (wstr1 == wstr2);
但没有运气。 VSVC2008 IDE 调试器中的字符串在视觉上是相同的。
问题:为什么比较方法总是给我假?
谢谢。
解决方案:非常感谢Alexandre C.
bool weirdEquals(const std::wstring& str, char const* c)
{
std::string c_str(c);
if (str.size() < c_str.size())
{
return false;
}
return std::equal(c_str.begin(), c_str.end(), str.begin());
}
PS。我做了一些改变。
What I have:
- [wstr1] 1 std::wstring value (e.g. "(GMT+04:30) Kabul")
- [str2] 1 char* filled with the same data as std::wstring (with the same text)
I'm simply trying to compare this two value - but the following code doesn't appear to work. KMP.
Here is the converter from char* to std::wstring,
std::wstring TimeZone::TimeZone::char2ws(const char* src)
{
/* Convert to a wchar_t*
* http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx
*/
// Convert to a wchar_t*
size_t origsize = strlen(src) + 1;
const size_t newsize = 512;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, src, _TRUNCATE);
return wcstring;
}
after that I made simple compare with
// wstr1 is std::wstring, with "(GMT+04:30) Kabul" string
// wstr2 = char2ws(str2),
return (wstr1 == wstr2);
And no luck. String in VSVC2008 IDE debugger are visually equal.
Question: why do compare method gives me false always?
Thanks.
Solution: great thanks to Alexandre C.
bool weirdEquals(const std::wstring& str, char const* c)
{
std::string c_str(c);
if (str.size() < c_str.size())
{
return false;
}
return std::equal(c_str.begin(), c_str.end(), str.begin());
}
PS. I made some changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确定只有 ASCII 字符,请像这样进行比较。
否则,您可以将自定义(区域设置相关?)比较器传递给
std::equal
。Compare like this, if you're sure that you have only ASCII characters.
Otherwise, you can pass a custom (locale dependant ?) comparer to
std::equal
.要比较字符串,您需要通过相同的字符集来比较它们。
wchar_t
和char
显然使用不同的字符集,因此您无法使用典型的字符对字符字符串比较来比较它们。通常,您可以将
char*
字符串转换为wchar_t*
字符串(使用mbtowcs
或MultiByteToWideChar
),以便您可以像您一样将它们作为 Unicode 字符串进行比较。但我怀疑在你的情况下,你只期望简单的 ASCII 字符匹配,在这种情况下你可以进行逐个字符的比较,将
wchar_t
s 与char
s。注意:不要使用强制转换来执行此操作。至于为什么你的转换不起作用,请参阅@Erik的答案。
To compare strings, you need to compare them via the same character set.
wchar_t
andchar
are clearly using different character sets so you cannot compare them using a typical char-for-char string comparison.Typically, you would convert the
char*
string to awchar_t*
string (usingmbtowcs
orMultiByteToWideChar
) so you can compare them both as Unicode strings, as you are doing.But I suspect in your case though, you only ever expect simple ASCII characters to match, in which case you can do a char-by-char comparison, comparing
wchar_t
s tochar
s. Note: do not use a cast to do this.As to why your conversion is not working, see @Erik's answer.
您需要将
newsize
而不是origsize
传递给 mbstowcs_s。另外,考虑使用:
You need to pass
newsize
, notorigsize
, to mbstowcs_s.Also, consider using: