如何在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
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短