Created
March 10, 2014 13:43
-
-
Save RyosukeMiyahara/9465124 to your computer and use it in GitHub Desktop.
C++でString (char配列)をintegerに変換します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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