Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Converting string to integer

Posted on 2003-03-15
2
Medium Priority
?
222 Views
Last Modified: 2010-04-01
Hello experts,

How can I convert a string (of type string, not char*) into an integer?
Is there an API function for this? atoi works only on char*, however, converting all the strings to char* won't be efficient (I have a lot of strings :-) )

Any suggestions?
0
Comment
Question by:Makover
  • 2
2 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 200 total points
ID: 8145640
Using c_str casts the string to a const char *, so you could say:
std::string s = "231";
int i = atoi(s.c_str());
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8145642
By the way, c_str just lets you get at the underlying buffer's char *, it doesn't create a new one or anything, so the efficiency is probably close to as good as you can get.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question