C++ 入門指南 4.01
練習 15.10 參考程式 - 練習字串轉換成數字
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中相關的 string 程式
#include <string>
// cout 為 std 中的輸出物件
using std::cout;
// endl 為 std 中的斷行符號
using std::endl;
// string 為 std 中的字串型態
using std::string;
// stoi 為 std 中的整數轉換函數
using std::stoi;
// stof 為 std 中的浮點數轉換函數
using std::stof;
int main(void) {
// 宣告字串變數 a
string a = "99.99";
// 印出變數 a
cout << a << endl;
// stirng 轉換成 int
cout << stoi(a) << endl;
// string 轉換成 float
cout << stof(a) << endl;
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise1510.cxx
編譯:g++ exercise1510.cxx
執行:./a.out
功能:C++入門指南單元十五的練習
作者:張凱慶 */
回到練習題目