#define STRINGCONV_H
#include <string>
#include <sstream>
template<typename T>
T fromString(const std::string& s)
{
std::istringstream is(s);
T t;
is >> t;
return t;
}
template<typename T>
std::string toString(const T& t)
{
std::ostringstream s;
s << t;
return s.str();
}
#endif
2 #include <iostream>
3 #include <complex>
4 using namespace std;
5
6 int main()
7 {
8 int i = 1234;
9 cout << "i == \"" << toString(i) << "\"\n";
10
11 float x = 567.89;
12 cout << "x == \"" << toString(x) << "\"\n";
13
14 complex<float> c(1.0, 2.0);
15 cout << "c == \"" << toString(c) << "\"\n";
16 cout << endl;
17
18 i = fromString<int>(string("1234"));
19 cout << "i == " << i << endl;
20 x = fromString<float>(string("567.89"));
21 cout << "x == " << x << endl;
22 c = fromString< complex<float> >(string("(1.0, 2.0"));
23 cout << "c == " << c << endl;
24
25 }