我在 C++ 中如何定義字串常量?


要在 C++ 中定義字串常量,你需要包含字串標頭檔案庫,然後使用該類和 const 關鍵字建立字串常量。

示例

#include<iostream>
#include<string>
int main() {
const std::string MY_STRING = "Hello World!";
std::cout << MY_STRING;
return 0;
}

輸出

以上程式碼將輸出以下結果:

Hello World!

請注意,如果你嘗試重新賦值給此變數,則會導致錯誤。

更新於:2020-06-19

13K+ 瀏覽

開啟您的 職業生涯

完成課程,獲取認證

開始
廣告
© . All rights reserved.