如何在数组上使用运算符L? (C ++,Visual Studio 2019) [英] How to use operator L on array? (C++, Visual Studio 2019)

查看:121
本文介绍了如何在数组上使用运算符L? (C ++,Visual Studio 2019)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第2部分:使用C ++编码字符(由 User123 ).

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

<-转到上一篇文章.

我昨天正在编写一些代码,并且 Paul Sanders .com/questions/60469861/why-do-i-always-get-%C5%98-inst-of-%C4%8D-c-visual-studio>这个问题告诉我有用的解决方案:告诉我不要使用std::cout << "something";,而要使用std::wcout << L"something";.

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:

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

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

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?

推荐答案

#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 字符),因此无法将其保存为.您需要将其保存在wstring中.要输出wstring,您需要将其传递给wcout,而不是cout.

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.

这篇关于如何在数组上使用运算符L? (C ++,Visual Studio 2019)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆