如何在C ++中将std :: string转换为LPCSTR?
在本节中,我们将看到如何将C++字符串(std::string)转换为LPCSTR。LPCSTR是(常量STRing的长指针)。它基本上是类似于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
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬