web analytics

Converting String to Integer / Double or Vice Versa with C++ Template Functions

Options

codeling 1595 - 6639
@2016-01-26 11:55:45

Convert String to Integer or Double

As we know, C++ standard library provide several standar functions to convert string to int or double.

  • atoi(): Convert a string to an integer;
  • atol(): Convert a string to long integer;
  • atof(): Convert a string to a double;

At here, I provide an alternative way to do all of the tasks in one function.  You could use the following template function that use istringstreams for converting string to int or double.

//---------------------------------------------------
// Convert string to typename T (e.g. int, double etc)
// e.g: int d = FromString<int>( s );

//---------------------------------------------------
#include <sstream>
template<typename T>
T FromString(const std::string& s)

{
  std::istringstream is(s);
  T t;
  is >> t;
  return t;
}

Overloaded istream:: operator>>

Extracts data from the stream and stores it in the parameter passed (right-value).

class members:

istream& operator>> (bool& val );
istream& operator>> (short& val );
istream& operator>> (unsigned short& val );
istream& operator>> (int& val );
istream& operator>> (unsigned int& val );
istream& operator>> (long& val );
istream& operator>> (unsigned long& val );
istream& operator>> (float& val );
istream& operator>> (double& val );
istream& operator>> (long double& val );
istream& operator>> (void*& val );
 
istream& operator>> (streambuf& sb );
 
istream& operator>> (istream& ( *pf )(istream&));
istream& operator>> (ios& ( *pf )(ios&));
istream& operator>> (ios_base& ( *pf )(ios_base&));

external operator>>  overloaded functions:

istream& operator>> (istream& is, char& ch );
istream& operator>> (istream& is, signed char& ch );
istream& operator>> (istream& is, unsigned char& ch );
 
istream& operator>> (istream& is, char* str );
istream& operator>> (istream& is, signed char* str );
istream& operator>> (istream& is, unsigned char* str );
@2016-01-26 12:27:21

Convert Integer or Double to String

C++ standard library also provide several standar functions to int to string.

  • itoa(): Convert integer to string;
  • ltoa(): Convert long integer value to string;
  • strtod(): Convert string to double-precision floating-point value

At here, I provide an alternative way to do all of the tasks in one function.  You could use the following template function that use ostringstreams for converting int or double to string.

//-------------------------------------------------------
// Convert typename t ( e.g. int or double etc) to string
// e.g: string s = ToString( d );

//-------------------------------------------------------
#include <sstream>
template<typename T>
std::string ToString(const T& t)

{
  std::ostringstream s;
  s << t;
  return s.str();
}

Overloaded ostream:: operator<<

Inserts data from the given parameter into the output stream.

class members:

ostream& operator<< (bool& val );
ostream& operator<< (short& val );
ostream& operator<< (unsigned short& val );
ostream& operator<< (int& val );
ostream& operator<< (unsigned int& val );
ostream& operator<< (long& val );
ostream& operator<< (unsigned long& val );
ostream& operator<< (float& val );
ostream& operator<< (double& val );
ostream& operator<< (long double& val );
ostream& operator<< (void*& val );
 
ostream& operator<< (streambuf& sb );
 
ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));

external operator<<  overloaded functions:

ostream& operator<< (ostream& os, char ch );
ostream& operator<< (ostream& os, signed char ch );
ostream& operator<< (ostream& os, unsigned char ch );
 
ostream& operator<< (ostream& os, const char* str );
ostream& operator<< (ostream& os, const signed char* str );
ostream& operator<< (ostream& os, const unsigned char* str );

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com