C++ 如何添加到 wstring?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4348392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add to a wstring?
提问by codefrog
This works with std::string
这适用于 std::string
std::string class::something(char* input) {
std::string s(input);
s = "hai! " + s;
return s;
}
But fails if I try the same thing with wstring
但是如果我用 wstring 尝试同样的事情就会失败
std::wstring class::something(wchar_t* input) {
std::wstring s(input);
s = "hai! " + s;
return s;
}
How do I do the same thing with std::wstring?
我如何用 std::wstring 做同样的事情?
回答by Adam Norberg
The problem here is types. A wstring isn't a string, but a quoted string constant is related to it (it is generally a const char*
), so
这里的问题是类型。wstring 不是字符串,而是引用的字符串常量与之相关(通常是 a const char*
),所以
s = "hai! " + s;
is actually a problem.
其实是个问题。
The value "hai! "
is of type const char*
, not type const wchar_t*
. Since const char*
is a basic type, it's searching for a global operator+
that operates const char*
and wstring
, which doesn't exist. It would find one for const wchar_t*
and wstring
, because std::basic_string<T>
, the template underyling type for both string
and wstring
(using char
and wchar_t
as the type parameter, respectively) also creates template methods for operator+ (const T*& s1, const basic_string<T> s2)
so that addition can work.
该值"hai! "
属于 type const char*
,而不是 type const wchar_t*
。由于const char*
是基本类型,因此它正在搜索不存在的operator+
操作const char*
and的全局wstring
变量。它会发现一个用于const wchar_t*
和wstring
,因为std::basic_string<T>
,模板underyling两种类型string
和wstring
(使用char
和wchar_t
分别为类型参数)也创造了模板方法operator+ (const T*& s1, const basic_string<T> s2)
,这样除了可以工作。
Therefore, you need to make "hai! " a wstring:
因此,您需要将 "hai!" 变成一个 wstring:
std::wstring class::something(wchar_t* input){
std::wstring s(input);
s = L"hai! " + s;
return s;
}
The L
prefix on a string constant, in Visual C++, defines it to be "long", and therefore a wstring. wstring
is actually basic_string<wchar_t>
, which is, because of the behavior of C++ templates, a completely different type from basic_string<char>
(a std::string
), so you can't combine the two.
L
在 Visual C++ 中,字符串常量的前缀将其定义为“long”,因此定义为 wstring。wstring
实际上basic_string<wchar_t>
是 ,也就是说,由于 C++ 模板的行为,与basic_string<char>
(a std::string
) 的类型完全不同,因此您不能将两者结合起来。
回答by Edward Strange
You use a wide character literal instead of a char character literal by prefixing "hai!" with L.
您可以通过前缀“hai!”来使用宽字符文字而不是字符文字文字。与 L。
回答by Zac Howland
Instead of having 2 versions of the code (1 for wide and 1 for multi-byte):
而不是有 2 个版本的代码(1 个用于宽字节,1 个用于多字节):
#ifdef _UNICODE
typedef std::wstring tstring;
typedef wchar_t tchar;
# define TEXT(s) L##s
#else
typedef std::string tstring;
typedef char tchar;
# define TEXT(s) s
#endif
tstring class::something(tchar* input)
{
tstring s(input);
s = TEXT("hai! ") + s;
return s;
}
This prevents having to rewriting code when you change your compiler string encoding compiler options.
这可以防止在更改编译器字符串编码编译器选项时必须重写代码。