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'
。
该方法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
书面条件。这篇文章详细解释了原因,并提供了更好的选择。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句