Boost 1.52.0からboost::lexical_cast()に、以下のオーバーロードが追加されました:
namespace boost { template <typename Target> Target lexical_cast(const AnyCharacterType* chars, std::size_t count); }
これは、任意の文字型の文字配列を、先頭N文字だけ変換対象にするものです。
以下のように使います:
#include <iostream> #include <boost/lexical_cast.hpp> int main() { int value = boost::lexical_cast<int>("12345", 3); std::cout << value << std::endl; }
123
ここでは、"12345"というchar配列の先頭3文字をintに変換しています。
参照:
boost::lexical_cast documentation