Functino Templates - Example - Convert a string to a number
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <typename T> void to_number(const string& S, T& number)
{
istringstream sin(S);
sin >> number;
}
int main()
{
string one("1");
string pi("3.14");
string unsigned_guy("5");
string floater("2.7");
string shortie("7");
string ll("1234567890123456789012345678901");
string longdouble("3.14159265358979323846");
int One;
double Pi;
unsigned int Unsigned_guy;
float Floater;
short Shortie;
long long LL;
long double LongDouble;
to_number(one,One);
to_number(pi,Pi);
to_number(unsigned_guy,Unsigned_guy);
to_number(floater,Floater);
to_number(shortie,Shortie);
to_number(ll,LL);
to_number(longdouble,LongDouble);
cout << One << ' ' << Pi << ' ' << Unsigned_guy << ' ' << Floater << ' ' << Shortie << endl;
cout << ll << ' ' << longdouble << endl;
return 0;
}
/***** Output *****
1 3.14 5 2.7 7
1234567890123456789012345678901 3.14159265358979323846
*********************/