프로그래밍 언어/boost

Boost.Lexical_cast

copynull 2014. 4. 2. 16:32

boost::lexical_cast

숫자를 문자열로 또는 그 반대로 변환시 사용


boost::bad_lexical_cast

형변환 타입 에러시 예외 처리



사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "stdafx.h"
#include <boost/lexical_cast.hpp>
 
int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        // number -> string
        cout << boost::lexical_cast<string>(12345) << endl;
 
        // string -> number
        cout << boost::lexical_cast<int>("54321") << endl;
 
        // string -> number (range)
        cout << boost::lexical_cast<int>("674ABC", 3) << endl;
 
        // Error
        cout << boost::lexical_cast<int>("ABCDE") << endl;
 
    }
    catch (boost::bad_lexical_cast& e)
    {
        cout << e.what() << endl;
    }
 
    return 0;
}
 


결과