C++と色々

主にC++やプログラムに関する記事を投稿します。

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