如何添加到wstring?

时间:2022-10-30 10:37:53

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做同样的事情?

3 个解决方案

#1


13  

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不是字符串,但引用的字符串常量与它相关(通常是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.

值“海!“是const char*类型,而不是const wchar_t*类型。由于const char*是一种基本类型,所以它正在搜索一个全局操作符+,它操作const char*和wstring,而这两个操作符并不存在。它会找到一个const wchar_t*和wstring,因为std: basic_string , string和wstring的模板underyling类型(分别使用char和wchar_t作为类型参数)也为操作符s1 +创建模板方法(const T*& const basic_string ),这样就可以进行加法。

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.

在Visual c++中,字符串常量的L前缀定义为“long”,因此是一个wstring。wstring实际上是basic_string ,因为c++模板的行为,完全不同于basic_string (std::string),所以不能将两者结合起来。

#2


7  

You use a wide character literal instead of a char character literal by prefixing "hai!" with L.

你使用宽字符文字而不是字符文字的前缀“hai!”与L。

#3


-1  

Instead of having 2 versions of the code (1 for wide and 1 for multi-byte):

而不是有两个版本的代码(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.

这可以避免在更改编译器字符串编码编译器选项时重写代码。

#1


13  

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不是字符串,但引用的字符串常量与它相关(通常是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.

值“海!“是const char*类型,而不是const wchar_t*类型。由于const char*是一种基本类型,所以它正在搜索一个全局操作符+,它操作const char*和wstring,而这两个操作符并不存在。它会找到一个const wchar_t*和wstring,因为std: basic_string , string和wstring的模板underyling类型(分别使用char和wchar_t作为类型参数)也为操作符s1 +创建模板方法(const T*& const basic_string ),这样就可以进行加法。

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.

在Visual c++中,字符串常量的L前缀定义为“long”,因此是一个wstring。wstring实际上是basic_string ,因为c++模板的行为,完全不同于basic_string (std::string),所以不能将两者结合起来。

#2


7  

You use a wide character literal instead of a char character literal by prefixing "hai!" with L.

你使用宽字符文字而不是字符文字的前缀“hai!”与L。

#3


-1  

Instead of having 2 versions of the code (1 for wide and 1 for multi-byte):

而不是有两个版本的代码(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.

这可以避免在更改编译器字符串编码编译器选项时重写代码。