Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(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)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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"));
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml