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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string stl problem 1

Status
Not open for further replies.

Tdrgabi

Programmer
Jun 24, 2004
43
RO
hi ... i need an urgent answer to a problem ...

i have a lot of numerical values ... into strings (stl string).

i need to convert it to "int" because i want to do some math stuff with them ....
so how can i convert string ------->>> int ?
 
Use old good atoi() from C/C++ library:
Code:
#include <cstdlib>
#include <string>
...
string s = "12345";
int i = atoi(s.c_str()); // c_str() string member function.
c_str() returns a pointer to C-like representation (with zero terminator) of a stl::string object...
 
Or stringstreams:
Code:
#include <sstream>
#include <string>
...
std::string s("12345 5.23 7");
std::istringstream istr(s);
int i, j;
double d;
istr >> i >> d >> j;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top