问题
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