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

沒有留言:

2024年React state management趨勢

輕量化 在過去Redux 是 React 狀態管理的首選函式庫。 Redux 提供了強大的功能和靈活性,但也帶來了一定的學習成本和複雜度。 隨著 React 生態的不斷發展,越來越多的開發者開始追求輕量化的狀態管理函式庫。 Zustand 和 Recoil 等庫以其簡單易用、性...