There are many features of C++ which make easier to code than of C.
For example is to convert a type of number to string, and vice versa. I used to
call function such as atoi() and its brothers and sisters to do the conversion,
but I think, by using the trait of C++ it looks cooler and easier.. hehehe..
After seeing example from cprogramming.com (I forget the exact page link), I
learn that to do conversion between string and number,
ostringstream/istringstream classes and input/output operator ( >>
and << ) can be used (by including sstream). To simplify the
usage, I create two functions numtostr() and strtonum() which convert any number
(integer, char, float, double, etc) to string, and string to any number. These
two functions are based on the example I’ve seen. I only add the template and a
little modification to make it easier to use.
Here is the code and the example (in total, there are 3 files: convert.h and convert.inl
contain the conversion function, and main.cpp
is the example).
file convert.h:
/**
file name: convert.h
*/
#ifndef MY_CONVERT_H
#define MY_CONVERT_H
#include <sstream>
using namespace std;
/* *
function name : numtostr()
used to convert T to string *
parameter:
i : variable with type T
return value: string contains the value of T
*/
template<class T>
string numtostr(const T i);
/* *
function name : strtonum()
used to convert string to T
parameter:
s : string to convert
o : the output (variable of T contains the converted value)
return value:
true means success, false means failed
*/
template<class T>
bool strtonum(const string &s, T &o);
#include "convert.inl"
#endif
file convert.inl:
template<class T>
string numtostr(const T i)
{
ostringstream output;
output << i << flush;
return output.str();
}
template<class T>
bool strtonum(const string &s, T &o)
{
istringstream input(s);
if (input >> o)
return true;
return false;
}
And here is the main.cpp:
#include
<iostream>
#include "convert.h"
int main()
{
double d = 0;
int i = 0;
char c = 0;
string s = "";
//string to double
s = "0.25";
if (strtonum(s, d))
cout << "converting
string " << s << " to
double : " << d << "
success" << endl;
else
cout << "converting
string " << s << " to
double : " << d << "
failed" << endl;
//string to integer
s = "25";
if (strtonum(s, i))
cout << "converting
string " << s << " to
integer : " << i << "
success" << endl;
else
cout << "converting
string " << s << " to
integer : " << i << "
failed" << endl;
//string to char
s = "A";
if (strtonum(s, c))
cout << "converting
string " << s << " to
char : " << c << "
success" << endl;
else
cout << "converting
string " << s << " to
char : " << c << "
failed" << endl;
s = "";
//double/integer/char to string
cout << "converting
double " << d << " to
string " << numtostr(d) << "
ok" << endl;
cout << "converting
integer " << i << " to
string " << numtostr(i) << "
ok" << endl;
cout << "converting
char " << c << " to
string " << numtostr(c) << "
ok" << endl;
return 0;
}
However, I’m still wondering whether these functions perform better or worse
than atoi() families. ^^’