stringstream实例
stringstream的具体作用稍后来总结,这里分享一个实例,从txt文档中读取数据,并对进行处理。
#include <iostream> #include <sstream> //stringstream的头文件 #include <fstream> #include <vector> #include <string> using namespace std; void main() { ifstream fin("d:\\desk\\face detection\\dataset\\train\\trainImageList.txt"); ofstream fout("d:\\desk\\face detection\\dataset\\train\\trainImageList1.txt"); char p_data[1024] = { 0 }; string filename; while (fin.getline(p_data, sizeof(p_data))) { vector<double> data; //stringstream resizeData; //resizeData.str(p_data); stringstream resizeData(p_data); //依照空格将数据分段 resizeData >> filename; //向filename流中写入值 while (resizeData) //数据流输完后就结束 { string a; resizeData >> a; data.push_back(atof(a.c_str())); } //data[0] 不变 double temp = data[1]; data[1] = data[2]; data[2] = data[3]; data[3] = temp; //使其为一个正方形 double w, h; double differenceValue = 0.0; w = data[2] - data[0]; h = data[3] - data[1]; differenceValue = abs(w - h); if (w > h) data[3] += differenceValue; else data[2] += differenceValue; fout << filename << " "; vector<double>::iterator it = data.begin(); for (; it != data.end()-1; it++) { fout << *it << " "; } fout << endl; } }