args_to_numbers.cpp 772 B

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