Linux c++ string转其他类型

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

template<class T>
T fromString(const string &str)
{
    istringstream is(str);
    T v;
    is >> v;
    return v;
}

int main()
{
    string sa = "3";
    string saaa = "33.3";
    int aa = 0;
    float bb = 0;
    aa = fromString<int>(sa);
    bb = fromString<float>(saaa);

    cout << aa << endl;
    cout << bb << endl;
    return 0;
} 
posted @ 2019-06-10 16:47  茫茫明明  阅读(1488)  评论(0编辑  收藏  举报