如何在 C++ 中將 int 轉換為字串?


你可以使用 C 語言中的 itoa 函式將 int 轉換為字串。

 示例

#include<iostream>
int main() {
   int a = 10;
   char *intStr = itoa(a);
   string str = string(intStr);
   cout << str;
}

輸出

這會輸出 −

10

這會將整數轉換為字串。在 C++11 中,添加了一種新方法 to_string,可以用於相同目的。你可以按照以下方法使用它 −

示例

#include<iostream>
using namespace std;
int main() {
   int a = 10;
   string str = to_string(a);
   cout << str;
}

輸出

這會輸出 −

10

更新於: 2020 年 6 月 24 日

3K+ 觀看

開啟你的 職業生涯

完成課程並獲得認證

立即開始
廣告