20070628

C++: numerical to string convertion using Boost lexical_cast.

#include <string>
#include
"boost/lexical_cast.hpp"

using namespace std;
// class ~ typename = same thing
template <class T>
string to_string(const T& arg) {
try {
return boost::lexical_cast<string>(arg);
}
catch(boost::bad_lexical_cast& e) {
return "";
}
}
Παράδειγμα χρήσης:
float float_nr=31212.434;
string float_str=to_string(float_nr);
// float_str = "31212.43"

Δεν υπάρχουν σχόλια:

eggs.in.art (my non-technical blog)