How to parse a string to an int in C++?

前端 未结 17 1757
忘了有多久
忘了有多久 2020-11-21 11:01

What\'s the C++ way of parsing a string (given as char *) into an int? Robust and clear error handling is a plus (instead of returning zero).

17条回答
  •  遥遥无期
    2020-11-21 12:03

    You can use Boost's lexical_cast, which wraps this in a more generic interface. lexical_cast(Source) throws bad_lexical_cast on failure.

提交回复
热议问题