【发布时间】:2017-01-11 02:36:23
【问题描述】:
在 c++ 中,我们有 std::to_string 将 int/float/double 转换为字符串。所以只是为了测试我对模板的理解,我尝试了下面的代码:
#include "iostream"
#include "sstream"
#include "string"
using std::cout;
template <typename T>
std::string getString(const T& data){
std::stringstream ss;
cout << '\n' << data << '\n';
ss << data;
std::string s;
ss >> s;
return s;
}
int main(int argc , char** argv){
cout << getString(1.0000011);
cout <<' '<<std::to_string(1.0000011);
return 0;
}
但是,输出没有意义,to_string 给了我1.0000011,而getString 得到 1 并给了我 1。因为我使用模板不应该 getString 得到 1.0000011 并给我也是1.0000011?
【问题讨论】: