(C++) IsInt
February 24, 2017 · View on GitHub
(C++) IsInt
IsInt is a checking code snippet to check if a std::string can be converted to an integer.
CanCast and CanLexicalCast are more general versions of IsInt.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
Qt project file: ./CppIsInt/CppIsInt.pro
include(../../ConsoleApplication.pri) include(../../Libraries/Boost.pri) SOURCES += main.cpp
./CppIsInt/main.cpp
#include <sstream> ///IsInt determines if std::string can be converted to integer. ///From http://www.richelbilderbeek.nl/CppIsInt.htm bool IsIntStl(const std::string& s) { std::istringstream i(s); int temp{0}; i >> temp; if (!i) return false; char c{'\0'}; //Should be at end, if not, the string contained more than just a number i >> c; if (i) return false; return true; } #include <boost/lexical_cast.hpp> ///IsInt determines if std::string can be converted to integer. ///From http://www.richelbilderbeek.nl/CppIsInt.htm bool IsIntBoost(const std::string& s) noexcept { try { boost::lexical_cast<int>(s); return true; } catch (boost::bad_lexical_cast&) { return false; } } #include <cassert> int main() { assert(IsIntStl("3")); assert(IsIntStl("0")); assert(IsIntStl("-23")); assert(!IsIntStl("a")); assert(!IsIntStl("2+fire")); assert(IsIntBoost("3")); assert(IsIntBoost("0")); assert(IsIntBoost("-23")); assert(!IsIntBoost("a")); assert(!IsIntBoost("2+fire")); }