Convert skills in C++, including int, double, float, bool with string

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

int main()
{
	stringstream ss;
	/************ int->string ************/
	int i = 12;
	string str;
	ss << i;
	ss >> str;
	cout << str << endl;
	/************ string->int ************/
	ss.clear();  // must
	str = "130";
	ss << str;
	ss >> i;
	cout << i << endl;
	/************ double->string ********/
	ss.clear();  // must
	double d = 1.2345;
	ss << d;
	ss >> str;
	cout << str << endl;
	/************ string->double ********/
	ss.clear();  // must
	str = "5.4321";
	ss << str;
	ss >> d;
	cout << d << endl;
	/************ bool->string ***********/
	ss.clear();  // must
	bool b = true;
	ss << b;
	ss >> str;
	cout << str <<endl;
	/************ string->bool ***********/
	ss.clear();  // must
	str = "0";
	ss << str;
	ss >> b;
	b ? cout << "true" : cout << "false";

	return 0;
}

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