Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STL string to integer conversion

Status
Not open for further replies.

amessbee

Programmer
Mar 15, 2006
1
0
0
US

I was wondering if there is a function provided by MS STL, that helps convert string to int(any numeric) type...

looking for your answer,
mudassir!

p.s: I know about atoi, but I want some STL provided function. It may be member function of some class or generic function as in <algorithm>
thanks
 
C++ Std RTL inherits all data conversion routines from C; no these category routines in STL.
You may use strstrem:
Code:
#include <strstream?
string s;
int x;
...
{ istrstream ss(s.c_str()); ss >> x; }
I think, atoi() is much more preferable in that case (it's member of C++ Standard RTL).
 
strstream is deprecated. Use stringstreams in <sstream> instead (which works with standard strings directly):
Code:
#include <sstream>
// ...
std::string s("-216");
int x;

std::istringstream ss(s);
ss >> x; // x is now -216

More information:
See 39.2 in the C++ Faq-Lite
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top