C++ 入門指南 4.01
練習 14.10 參考程式 - 練習標準程式庫 string 的 to_string()
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中相關的型態資訊程式
#include <typeinfo>
// 引入標準程式庫中相關的 string
#include <string>
// cout 為 std 中的輸出物件
using std::cout;
// endl 為 std 中的斷行符號
using std::endl;
// to_string 為 std 中轉換字串的函數
using std::to_string;
int main(void) {
// 宣告數字與字元變數
short a1 = 1;
int b1 = 2;
float c1 = 3.0;
// 印出利用 to_string 轉換 short 之後的型態
cout << typeid(to_string(a1)).name() << endl;
// 印出利用 to_string 轉換 int 之後的型態
cout << typeid(to_string(b1)).name() << endl;
// 印出利用 to_string 轉換 float 之後的型態
cout << typeid(to_string(c1)).name() << endl;
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise1410.cxx
編譯:g++ exercise1410.cxx
執行:./a.out
功能:C++入門指南單元十四的練習
作者:張凱慶 */
回到練習題目