int nach string
-
Hallo zusammen,
habe letztens eine Möglichkeit gesucht einen int Wert in einen std::string zu konvertieren. Habe hier im Forum nicht viel gefunden und google liefert auch viel Müll.
Schließlich fand ich das hier (Quelle: http://cpp.enisoc.com/faq/general/ )
Q. How do I convert from int to string?
A. Converting from int to string is a little more complicated than converting a string to int. There are no standard C++ functions for this, but there is a way to do it using standard stringstreams. The class stringstream is declared in header sstream and is used like this:
std::stringstream ss; std::string str; ss << 31337; ss >> str;The variable str now contains "31337".
Wollte ich nur mal anmerken und würde mich freuen, falls jemand noch was elegenateres findet.
TechM
-
In der FAQ des C++ Forums hättest du genau die selbe Lösung gefunden.
-
Eleganter wird's nur mit boost::lexical_cast<> was im prinzip genau das selbe macht
-
Was gefällt dir an der Lösung nicht?
Es gibt auch noch lexical_cast aus der boost-Bibliothek.
-
boost::lexical_cast
-
Also etwas eleganter finde ich doch den ostringstrem:
std::ostringstream ss; ss << 31337; // string steht jetzt in ss.str()Gruß,
Simon2.