Steve said:
Hi,
How can I convert a C++ string to an integer and then an integer back to
a string? Is there built-in support for this?
Thanks,
Yes lots of support. But what do you mean by string? For C style strings you
can use sprintf (int to string) or sscanf (string to int). For C++ strings
you use stringstream classes.
#include <sstream>
#include <string>
using namespace std;
// string to int
string some_string;
istringstream buffer(some_string);
int some_int;
buffer >> some_int;
// int to string
int some_int;
ostringstream buffer;
buffer << some_int;
string some_string = buffer.str();
You might notice that string streams use exactly the same reading and
writing methods as other streams (cin, cout etc). This is not a coincidence,
and makes string streams useful for a lot of things besides converting
strings to integers.
john