【发布时间】:2011-02-25 00:35:27
【问题描述】:
伙计们,我正在从文件中读取并输入到向量中,但我不断收到一个弹出框:“向量员工超出范围!”错误。就像我试图访问传递向量中的最后一个索引一样,但如果是这种情况,我看不到......任何帮助表示赞赏 文本文件:
123 vazquez 60000
222 james 100000
333 jons 50000
444 page 40000
555 plant 40000
代码:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
struct employees { int id; string lname; double salary; };
void getData(vector<employees>& list, ifstream& inf);
int i = 0;
int main()
{
string filename("file2.txt");
vector<employees> list;
ifstream inf;
inf.open(filename);
getData(list, inf);
inf.close();
for(unsigned int j = 0; j < list.size(); j++)
{
cout << list[j].id << " " << list[i].lname << endl;
}
system("pause");
return 0;
}
void getData(vector<employees>& list, ifstream& inf)
{
int i = 0;
while(inf)
{
inf >> list[i].id >> list[i].lname >> list[i].salary;
i++;
}
}
【问题讨论】:
标签: c++