读取字符串中的数字
原文链接:https://blog.csdn.net/qq_35976351/article/details/84799484
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
string str("55.5818061829 119.6388702393 22.33");
double t;
istringstream iss;
iss.str(str);
while(iss>>t) {
cout<<t<<endl;
}
return 0;
}
文件也能像“流”一样操作
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
string str("55.5818061829 119.6388702393 22.33");
double t;
istringstream iss;
iss.str(str);
while(iss>>t) {
cout<<t<<endl;
}
return 0;
}
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
istringstream iss;
string s;
double t;
// 按行读取,每行的结束是回车区分
fin.open("transform.txt");
while(getline(fin, s)) {
iss.clear();
iss.str(s);
while(iss>>t) {
cout<<t<<" ";
}
cout<<endl;
}
return 0;
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步