1 | // Copyright Antony Polukhin, 2013-2024. |
2 | |
3 | // Distributed under the Boost Software License, Version 1.0. |
4 | // (See the accompanying file LICENSE_1_0.txt |
5 | // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) |
6 | |
7 | //[lexical_cast_args_example |
8 | //`The following example treats command line arguments as a sequence of numeric data |
9 | |
10 | #include <boost/lexical_cast.hpp> |
11 | #include <vector> |
12 | |
13 | int main(int /*argc*/, char * argv[]) |
14 | { |
15 | using boost::lexical_cast; |
16 | using boost::bad_lexical_cast; |
17 | |
18 | std::vector<short> args; |
19 | |
20 | while (*++argv) |
21 | { |
22 | try |
23 | { |
24 | args.push_back(x: lexical_cast<short>(arg: *argv)); |
25 | } |
26 | catch(const bad_lexical_cast &) |
27 | { |
28 | args.push_back(x: 0); |
29 | } |
30 | } |
31 | |
32 | // ... |
33 | } |
34 | |
35 | //] [/lexical_cast_args_example] |
36 | |