Home c++ Converting string to integer

Converting string to integer

Author

Date

Category

Please tell me the functions for converting string type to integer in C++
Here is an example, actually you need to drive a number from S into N

# include & lt; string & gt;
int main ()
{
  string S = "74";
  int N;
  return 0;
}

Answer 1, authority 100%

# include & lt; stdlib.h & gt;
n = atoi (S.c_str ());

Answer 2, authority 75%

# include & lt; iostream & gt;
#include & lt; sstream & gt;
#include & lt; string & gt;
using namespace std;
int main () {
  string S = "74";
  istringstream iss (S, istringstream :: in);
  int val;
  iss & gt; & gt; val;
  return 0;
}

Answer 3, authority 50%

# include & lt; iostream & gt;
#include & lt; string & gt;
using namespace std;
void main () {
  string str = "1234567890";
  int num = NULL;
  for (int i = 0; i & lt; str.length (); i ++) {
    num = num * 10 + str [i] - 0x30;
  }
  cout & lt; & lt; num;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions