?
Solved

Converting string to integer

Posted on 2003-03-15
2
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

764 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