#include <iostream>
using namespace std;
int main() {
std::wstring str = L"u00A2";
std::wcout << str;
return 0;
}
为什么这不起作用?怎么解呢?
这不起作用,因为在默认的C语言环境中,没有对应于U+00A2的字符。
如果您使用的是标准的ubuntu安装,那么您的用户区域设置很可能使用比US-ASCII更大的字符集,很可能是用UTF-8编码的Unicode。因此,您只需要切换到环境中指定的区域设置,如下所示:
#include <iostream>
/* locale is needed for std::setlocale */
#include <locale>
#include <string>
int main() {
/* The following switches to the locale specified
* by the LC_ALL environment variable.
*/
std::setlocale (LC_ALL, "");
std::wstring str = L"u00A2";
std::wcout << str;
return 0;
}
如果您使用std::string
而不是std::wstring
和std::cout
而不是std::wcout
,那么您不需要setlocale
,因为不需要翻译(提供控制台期望UTF-8)。