#include <sstream> int main(int argc, char *argv[]) { double f = 0.0; std::stringstream ss; std::string s = "3.1415"; ss << s; ss >> f; cout << f; }
Itβs good that this solution also works for others, for example, for ints, etc.
If you want to reuse the same buffer, you must make ss.clear between them.
There is also a shorter solution in which you can initialize the value to a string stream and simultaneously clear it to a double:
#include <sstream> int main(int argc, char *argv[]){ stringstream("3.1415")>>f ; }
source share