【问题标题】:How to convert a std::string to L data type如何将 std::string 转换为 L 数据类型
【发布时间】:2021-02-26 00:42:42
【问题描述】:

您好,我开始学习 c++,我正在尝试了解以 L 开头的数据类型,例如

auto something = L"Monday";

什么数据类型是自动的? 以及如何将std::string 转换为这种数据类型?

std::string my_string = "Hello World"; ? something = L"Hello World"

【问题讨论】:

  • Thx,所以 L"" 是 wchar_t*,是否可以将字符串转换为 wchar_t*?
  • 这是一个wide string literal。讨论了herestd::string 扩展为std::wstring 的几种方法。
  • L"" 不是wchar_t*。它是const wchar_t[1],在大多数情况下衰减为const wchar_t*。有时,差异至关重要。

标签: c++


【解决方案1】:

auto 不是数据类型。它是一个占位符,得到deduced depending on the initializer used

在您的情况下,初始化程序是 const wchar_t[size] 类型的 wide string-literal,在用于初始化变量时衰减为 const wchar_t*

宽字符串可以存储在std::wstring中。

如何将std::string(窄字符串)转换为宽字符串取决于源的字符编码。

不管怎样,也有很多人问过这个问题:

C++ Convert string (or char*) to wstring (or wchar_t*)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多