How to use operator L on array? (C++, Visual Studio 2019)

拜拜、爱过 提交于 2020-04-10 06:29:19

问题


Part 2 on encoding characters in C++ (by User123).

<- Go to the previous post.

I was yesterday making some code, and Paul Sanders in this question told me useful solution: He told me not to use std::cout << "something"; but to use std::wcout << L"something";.

But I have another problem. Now I want to do something like this (some special characters, but in array):

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

But now I get something really unusual:

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

If I add L in front of the array, I get (Visual Studio 2019):

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

How can I represent these special characters but in the array?


回答1:


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

L can only be applied directly to string literals. The result is a string literal of type wchar_t[] (wide character) rather then the usual char_t[] (narrow character), so you cannot save it in a string. You need to save it in a wstring. And to output a wstring you need to pass it to wcout, not cout.



来源:https://stackoverflow.com/questions/60474063/how-to-use-operator-l-on-array-c-visual-studio-2019

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!