如何从文本文件中读取数据并将其加载到向量中?
How can I read the data from text file and load it into the vector?
我想使用结构从文本文件中读取数据并将其加载到向量中。
所以我写了下面的代码来做这个但是我编译失败了。不知道怎么回事
<我做了什么>
- 文本文件包含如下数据;
835,5,0,0,1,1,8.994,0
(整数数组[3],整数,整数,整数,整数,整数,浮点数,布尔值)
2.I 声明结构包含以下数据类型以将数据加载到向量中;
struct unfinished
{
int ans[3]; // contains answer
int max;
int st;
int ba;
int outs;
int tri;
float elapsed;
bool fin;
};
3.I写了一段代码读取一条数据如下;
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct unfinished
{
int ans[3];
int max;
int st;
int ba;
int outs;
int tri;
float elapsed;
bool fin;
};
vector<unfinished> read_rec(istream & is)
{
vector<unfinished> rec;
int ans[3];
int max, st, ba, outs, tri;
float elap;
bool fini;
while (is >> ans[0] >> ans[1] >> ans[2] >> max >> st >> ba >> outs >> tri >> elap >> fini)
{
rec.emplace_back(ans[0], ans[1], ans[2], max, st, ba, outs, tri, elap, fini);
}
return rec;
}
int main(void)
{
ifstream infile("unfin_rec.txt");
auto unfin = read_rec(infile);
vector<unfinished>::const_iterator it;
for (it = unfin.begin(); it != unfin.end(); it += 1)
{
cout << it->ans[0] << it->ans[1] << it->ans[2] << "," << it->max << "," << it->st << "," << it->ba <<","<<it->outs<<","<<it->tri<<","<<it->elapsed<<","<<it->fin<< endl;
}
system("pause");
return 0;
}
我未能编译此代码。错误消息是:>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600): error C2661: 'unfinished::unfinished' : 没有重载函数需要 10 个参数
同样,我无法理解这条消息的含义。请帮忙!
谢谢,
世亨
您需要添加一个带有 10 个参数并填充结构成员的构造函数,如下所示:
struct unfinished
{
unfinished(int a0, int a1, int a2,
int m, int s, int b, int o, int t,
float e, bool b):
max(m), st(s), ba(b), outs(o), tri(t), elap(e), fini(b) {
ans[0]=a0, ans[1]=a1, ans[2]=a2;
}
....
};
我想使用结构从文本文件中读取数据并将其加载到向量中。
所以我写了下面的代码来做这个但是我编译失败了。不知道怎么回事
<我做了什么>
- 文本文件包含如下数据;
835,5,0,0,1,1,8.994,0
(整数数组[3],整数,整数,整数,整数,整数,浮点数,布尔值)
2.I 声明结构包含以下数据类型以将数据加载到向量中;
struct unfinished
{
int ans[3]; // contains answer
int max;
int st;
int ba;
int outs;
int tri;
float elapsed;
bool fin;
};
3.I写了一段代码读取一条数据如下;
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct unfinished
{
int ans[3];
int max;
int st;
int ba;
int outs;
int tri;
float elapsed;
bool fin;
};
vector<unfinished> read_rec(istream & is)
{
vector<unfinished> rec;
int ans[3];
int max, st, ba, outs, tri;
float elap;
bool fini;
while (is >> ans[0] >> ans[1] >> ans[2] >> max >> st >> ba >> outs >> tri >> elap >> fini)
{
rec.emplace_back(ans[0], ans[1], ans[2], max, st, ba, outs, tri, elap, fini);
}
return rec;
}
int main(void)
{
ifstream infile("unfin_rec.txt");
auto unfin = read_rec(infile);
vector<unfinished>::const_iterator it;
for (it = unfin.begin(); it != unfin.end(); it += 1)
{
cout << it->ans[0] << it->ans[1] << it->ans[2] << "," << it->max << "," << it->st << "," << it->ba <<","<<it->outs<<","<<it->tri<<","<<it->elapsed<<","<<it->fin<< endl;
}
system("pause");
return 0;
}
我未能编译此代码。错误消息是:>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600): error C2661: 'unfinished::unfinished' : 没有重载函数需要 10 个参数
同样,我无法理解这条消息的含义。请帮忙!
谢谢,
世亨
您需要添加一个带有 10 个参数并填充结构成员的构造函数,如下所示:
struct unfinished
{
unfinished(int a0, int a1, int a2,
int m, int s, int b, int o, int t,
float e, bool b):
max(m), st(s), ba(b), outs(o), tri(t), elap(e), fini(b) {
ans[0]=a0, ans[1]=a1, ans[2]=a2;
}
....
};