如何在 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