記如何解決藍橋杯中to_string和atoi等無法使用的問題

轉自:https://www.cnblogs.com/A-Little-Nut/p/10311316.html

代碼:

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
  //int 轉 string
  int number = 12;
  string str;
  stringstream ss;
  ss << number;
  ss >> str;
  cout << str;
  //注意,此時這個流中還留有之前流入的數據。
  ss.clear();  //清楚這個流中殘留的數據
  //string 轉 int
  string str2 = "13";
  int num;
  ss << str2;
  ss >> num;
  cout << num;

  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章