【发布时间】:2017-03-01 14:28:15
【问题描述】:
在 C++ 中,我想从字符串中获取值。
我知道有 stoi、stol、stof 之类的函数...
但是我可以写一个包含所有这些的函数吗?喜欢使用模板推断数据类型?
template<typename T>
T fromString(const std::string& s){
// do something here
// call stoi, stof according to typename T
}
string si = "1234";
int integer = fromString<int>(si);
string sf = "1234.1234";
float float_point = fromString<float>(sf);
【问题讨论】:
-
是的,你可以这样做。
-
@JohnnyMopp 他想要字符串中的值,而不是值中的字符串。
-
@mascoj 好的。错过了……呃。
标签: string