如何在数组上使用运算符L? (C ++,Visual Studio 2019)

问题描述 投票:0回答:1

第2部分使用C ++编码字符(由User123进行。]

<- Go to the previous post.

[我昨天正在编写一些代码,Paul Sanders中的this question告诉了我有用的解决方案:他告诉我不要使用std::cout << "something";,而要使用std::wcout << L"something";

但是我还有另一个问题。现在我想做这样的事情(一些特殊字符,但在数组中):

#include <iostream>
using namespace std;
string myArray[2] = { "łŁšđřžőšě", "×÷¤ßł§ř~ú" };
int main()
{
    cout << myArray[0] << endl << myArray[1];
    return 0;
}

但是现在我得到了非常不寻常的东西:

│úÜ­°×§Üý
θĄ▀│ž°~˙

如果在数组前面添加L,则会得到(Visual Studio 2019):

C++ initialization with '{...}' expected for aggregate object

我如何在数组中表示这些特殊字符?

c++ encoding utf-8 visual-studio-2019
1个回答
0
投票
#include <iostream>
using namespace std;
wstring myArray[2] = { L"łŁšđřžőšě", L"×÷¤ßł§ř~ú" };
int main()
{
    wcout << myArray[0] << endl << myArray[1];
    return 0;
}

L仅可直接应用于字符串文字。结果是类型为wchar_t[]wide字符)的字符串文字,而不是通常的char_t[]narrow字符),因此无法将其保存在string中。您需要将其保存在wstring中。要输出wstring,您需要将其传递给wcout,而不是cout

© www.soinside.com 2019 - 2024. All rights reserved.