如何将 std::string 转换为 C++ 中的 LPCWSTR?
在本节中,我们将了解如何将 C++ 宽字符串 (std::wstring) 转换为 LPCWSTR。LPCWSTR 是(指向常宽字符串的长指针)。它基本上是包含宽字符的字符串。因此,通过将宽字符串转换为宽字符数组,我们可以获得 LPCWSTR。此 LPCWSTR 由 Microsoft 定义。所以为了使用它们,我们必须将 Windows.h 头文件包含到我们的程序中。
要将 std::wstring 转换为宽字符数组类型字符串,我们可以使用名为 c_str() 的函数以使其成为 C 语言形式的字符串并指向宽字符字符串。
示例代码
#include<iostream>
#include<Windows.h>
using namespace std;
main(){
wstring my_str = L"Hello World";
LPCWSTR wide_string ; //define an array with size of my_str + 1
wide_string = my_str.c_str();
wcout << "my_str is : " << my_str <<endl;
wcout << "Wide String is : " << wide_string <<endl;
}输出
my_str is : Hello World Wide String is : Hello World
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP