C++ã§æååãæ°å¤ã«å¤æããæ¹æ³
å ãã¿ã«è¼ã£ã¦ããå¤ææ¹æ³ã使ã£ããµã³ãã«ãæ¸ãã¦ã¿ãã*1
atoi
#include <cstdlib> #include <iostream> #include <string> #include <typeinfo> int main() { const std::string str("123"); auto num = std::atoi(str.c_str()); std::cout << typeid(num).name() << " : " << num << std::endl; }
strtol
#include <cstdlib> #include <iostream> #include <string> #include <typeinfo> int main() { const std::string str("123"); char* e = nullptr; auto num = std::strtol(str.c_str(), &e, 10); std::cout << typeid(num).name() << " : " << num << std::endl; }
sscanf
#include <cstdio> #include <iostream> #include <string> #include <typeinfo> int main() { const std::string str("123"); int num = 0; sscanf(str.c_str(), "%d", &num); std::cout << typeid(num).name() << " : " << num << std::endl; }
stoi
#include <iostream> #include <string> #include <typeinfo> int main() { const std::string str("123"); int num = std::stoi(str); std::cout << typeid(num).name() << " : " << num << std::endl; }
istringstream
#include <iostream> #include <sstream> #include <string> #include <typeinfo> int main() { const std::string str("123"); std::istringstream iss(str); int num = 0; iss >> num; std::cout << typeid(num).name() << " : " << num << std::endl; }
boost::lexical_cast
#include <boost/lexical_cast.hpp> #include <iostream> #include <string> #include <typeinfo> int main() { const std::string str("123"); auto num = boost::lexical_cast<int>(str); std::cout << typeid(num).name() << " : " << num << std::endl; }
boost::spirit::qi
#include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> #include <typeinfo> int main() { const std::string str("123"); int num = 0; boost::spirit::qi::parse(str.begin(), str.end(), boost::spirit::qi::int_, num); std::cout << typeid(num).name() << " : " << num << std::endl; }
coerce
Boost.Coerceã¯ã¾ã æ£å¼ã«Boostå ¥ããã¦ããªãã¿ããã§ãã
#include <boost/coerce.hpp> #include <iostream> #include <string> #include <typeinfo> int main() { const std::string str("123"); auto num = boost::coerce::as<int>(str); std::cout << typeid(num).name() << " : " << num << std::endl; }
*1:èªåã§ã³ã¼ããæ¸ãããã¨ã«ããã®ããã°ã«å®è£ ä¹ã£ã¦ããã¨ã«æ°ã¥ãã¾ããâ¦orz