C++ stringstream(强制类型转换)

#include <iostream>
#include <string>
#include <sstream>


using namespace std;


int main(int argc,char** argv){

int value_int = 111;
double value_double = 1.11;
string value_string = "111";
bool value_bool = true;

stringstream ss;

ss.clear(); //int<--->double之间的转换 
ss << value_int;
double temp_double = 0.00;
cout << temp_double <<  " ";
ss >> temp_double;
cout << temp_double << endl;;

ss.clear(); //int<--->string之间的转换
ss << value_int; //调用两次是为了防止string类型和int类型的输出使读者混淆。 
ss << value_int;
string temp_string = "";
cout << temp_string << " ";
ss >> temp_string;
cout << temp_string << endl;

ss.clear(); //int<--->bool之间的转换
ss << value_bool;
bool temp_bool = false;
cout << temp_bool << " "; 
ss >> temp_bool;
cout << temp_bool << endl;


}
作者:蓝月

-------------------------------------------

个性签名:能我之人何其多,戒骄戒躁,脚踏实地地走好每一步

原文地址:https://www.cnblogs.com/viplanyue/p/12700761.html