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 << "\t";
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 << "\t";
ss >> temp_string;
cout << temp_string << endl;
ss.clear(); //int<--->bool之间的转换
ss << value_bool;
bool temp_bool = false;
cout << temp_bool << "\t";
ss >> temp_bool;
cout << temp_bool << endl;
}
#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 << "\t";
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 << "\t";
ss >> temp_string;
cout << temp_string << endl;
ss.clear(); //int<--->bool之间的转换
ss << value_bool;
bool temp_bool = false;
cout << temp_bool << "\t";
ss >> temp_bool;
cout << temp_bool << endl;
}