+-
由于其他问题,我试图在程序的其中一部分中使用vectors
而不是数组。我以前从未真正使用过它们。
这是代码的一部分:
#include <vector>
ifstream file("data.txt");
vector<int> beds;
vector<int> people;
vector<int> balconies;
while(!file.eof()){
file >> people.push_back();
file >> beds.push_back();
file >> balconies.push_back();
}
我不知道它是否会工作。无论如何,现在我有一个错误:No matching member function for call to 'push_back'
。
3
投票
投票
[std::vector::push_back
方法接受一个参数,它是要添加到向量末尾的值。因此,您需要将每个调用分为两个步骤:首先将值读取到int
中,然后将push_back
将该值读取到向量中。
while(!file.eof()){
int temp;
file >> temp;
people.push_back(temp);
file >> temp;
beds.push_back(temp);
file >> temp;
balconies.push_back(temp);
}
如评论中所述,我建议反对您所写的while
条件。 This post详细解释了原因,并提供了更好的选择。
1
投票
投票
将输入数据先存储在变量内,然后推送该变量
int example;
file >> example;
people.push_back(example);
或使用std :: istream_iterator