2010/03/22

[C++] string to int, int to string, string to double, double to string

Convert.h

#ifndef CONVERT_H_
#define CONVERT_H_
#include <sstream>
#include <string>
using namespace std;
class Convert{
public:
    static int str2int(const string &str);
    static string int2str(const int n);
    static double str2double(const string &str);
    static string double2str(const double n);
};
#endif /*CONVERT_H_*/


Convert.cpp

#include "Convert.h"
int Convert::str2int (const string &str) {
    int n;
    stringstream ss(str);   
    ss >> n;       
    return n;
};   
string Convert::int2str (const int n) {
    stringstream ss;
    ss << n;
    return ss.str();
}
double Convert::str2double(const string &str){
    double n;
    stringstream ss(str);   
    ss >> n;       
    return n;
}
string Convert::double2str(const double n){
    stringstream ss;
    ss << n;
    return ss.str();
}

沒有留言:

Buddhism and Software Developer

In today's fast-paced society, we are often surrounded by work, goals, and external pressures. However, the wisdom found in Buddhism off...