如何在 C++ 中将 std::string 转换为 LPCSTR?
在本节中,我们将了解如何将 C++ 字符串 (std::string) 转换为 LPCSTR。LPCSTR 是(指向常量字符串的长指针)。它基本上是 C 中类似的字符串。因此,通过将字符串转换为字符数组,我们可以得到 LPCSTR。此 LPCSTR 是 Microsoft 定义的。因此,要使用它们,我们必须在程序中包含 Windows.h 头文件。
要将 std::string 转换为类似 C 的字符串,我们可以使用一个名为 c_str() 的函数。
示例代码
#include<iostream>
#include<Windows.h>
using namespace std;
main() {
string my_str = "Hello World";
LPTSTR long_string = new TCHAR[my_str.size() + 1]; //define
an array with size of my_str + 1
strcpy(long_string, my_str.c_str());
cout << "my_str is : " << my_str <<endl;
cout << "Long String is : " << long_string <<endl;
}输出
my_str is : Hello World Long String is : Hello World
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP