Skip to content

Instantly share code, notes, and snippets.

@RyosukeMiyahara
Created March 10, 2014 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyosukeMiyahara/9465124 to your computer and use it in GitHub Desktop.
Save RyosukeMiyahara/9465124 to your computer and use it in GitHub Desktop.
C++でString (char配列)をintegerに変換します。
#include <iostream> // cout, endl
#include <cstdlib> // atoi
// Convert string (char array) to integer
int main() {
std::string string123("123"); // This is string
// Convert string (char array) to integer using atoi
if (std::atoi(string123.c_str()) == 123) {
std::cout << "std::atoi(string123.c_str()) is 123(int)" << std::endl;
} else {
std::cout << "std::atoi(string123.c_str()) is not 123(int)" << std::endl;
}
// If string variable has no numeric value
std::string str("str");
std::cout << "atoi(str) = " << std::atoi(str.c_str()) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment