用文件流打开,然后按行读取,以空格为标志符切割字符串,把得到的字符串转换为int然后写入到一个容器进行操作。
#include
#include
#include
#include
using namespace std;
// 打开输入文件
ifstream in("sample.log");
if (!in)
{
cerr<< "error:unable to open input file"
<
string line;
//按行读取直到文件结束
while (getline(in,line))
{
vector
size_t pos = 1,pos2 = 0;
// 字符串分割写入到向量容器temp
while (pos >0)
{ pos = line.find(" ",pos);
temp.push_back(line.substr(pos2,pos-pos2));
pos++;
pos2 = pos;
}
}
in.close();
int n;
stringstream ss;
//字符串转换然后保存到向量容易temp2
for (int j = 0; j < temp.size(); j++)
{
ss<
temp2.push_back(n);
ss.clear();
}
//随手写的,自己拼凑着用