Skip to content

Instantly share code, notes, and snippets.

@timdrichards
Created September 24, 2012 15:22
Show Gist options
  • Save timdrichards/3776507 to your computer and use it in GitHub Desktop.
Save timdrichards/3776507 to your computer and use it in GitHub Desktop.
Convert string to int
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
// This is how you would convert a C++ string into an integer:
string str1 = "45";
stringstream sstr1(str1);
int x;
sstr1 >> x;
cout << "The x integer: " << x << endl;
char* str2 = "46";
stringstream sstr2(str2);
sstr2 >> x;
cout << "The x integer: " << x << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment