将文件中的整数读入 C++ 中的向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19602407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read integers from a file into a vector in C++
提问by user2922063
I am trying to read an unknown number of double values stored on separate lines from a text file into a vector called rainfall
. My code won't compile; I am getting the error no match for 'operator>>' in 'inputFile >> rainfall'
for the while loop line. I understand how to read in from a file into an array, but we are required to use vectors for this project and I'm not getting it. I appreciate any tips you can give on my partial code below.
我试图将存储在不同行中的未知数量的双精度值从文本文件读取到名为rainfall
. 我的代码无法编译;我收到no match for 'operator>>' in 'inputFile >> rainfall'
while 循环行的错误。我了解如何从文件中读入数组,但是我们需要为此项目使用向量,而我没有得到它。我很感激您可以在下面的部分代码中提供任何提示。
vector<double> rainfall; // a vector to hold rainfall data
// open file
ifstream inputFile("/home/shared/data4.txt");
// test file open
if (inputFile) {
int count = 0; // count number of items in the file
// read the elements in the file into a vector
while ( inputFile >> rainfall ) {
rainfall.push_back(count);
++count;
}
// close the file
回答by rcs
I think you should store it in a variable of type double
. Seems you are doing >>
to a vector, which is not valid. Consider the following code:
我认为您应该将其存储在类型为 的变量中double
。似乎您正在>>
对向量进行操作,这是无效的。考虑以下代码:
// open file
ifstream inputFile("/home/shared/data4.txt");
// test file open
if (inputFile) {
double value;
// read the elements in the file into a vector
while ( inputFile >> value ) {
rainfall.push_back(value);
}
// close the file
As @legends2k points out, you don't need to use the variable count
. Use rainfall.size()
to retrieve the number of items in the vector.
正如@legends2k 指出的那样,您不需要使用变量count
。使用rainfall.size()
检索的载体项目的数量。
回答by dasblinkenlight
You cannot use >>
operator to read in the whole vector. You need to read one item at a time, and push it into the vector:
您不能使用>>
运算符读取整个向量。您需要一次读取一项,并将其推入向量中:
double v;
while (inputFile >> v) {
rainfall.push_back(v);
}
You do not need to count the entries, because rainfall.size()
will give you the exact count.
您不需要计算条目,因为rainfall.size()
它将为您提供确切的计数。
Finally, the most C++ -ish way of reading a vector is with istream iterators:
最后,读取向量的最 C++-ish 方式是使用 istream 迭代器:
// Prepare a pair of iterators to read the data from cin
std::istream_iterator<double> eos;
std::istream_iterator<double> iit(inputFile);
// No loop is necessary, because you can use copy()
std::copy(iit, eos, std::back_inserter(rainfall));
回答by Charlie
You could also do:
你也可以这样做:
#include <algorithm>
#include <iterator>
...
std::istream_iterator<double> input(inputFile);
std::copy(input, std::istream_iterator<double>(),
std::back_inserter(rainfall));
...
assuming you like the STL.
假设你喜欢 STL。
回答by Snps
The input operator >>
is not defined for inputting double
s into a std::vector
.
输入运算符>>
未定义用于将double
s输入到 a 中std::vector
。
Instead construct the std::vector
with two tokenizing input iterators for the input file.
而是std::vector
使用两个标记化输入迭代器为输入文件构造。
Here's an example of how you can do it by using only 2 lines of code:
这是一个示例,说明如何仅使用2 行代码即可完成此操作:
std::ifstream inputFile{"/home/shared/data4.txt"};
std::vector<double> rainfall{std::istream_iterator<double>{inputFile}, {}};
Another solution is to define the input operator functionas:
另一种解决方案是将输入运算符函数定义为:
std::istream& operator>> (std::istream& in, std::vector<double>& v) {
double d;
while (in >> d) {
v.push_back(d);
}
return in;
}
Then you can use it as in your example:
然后你可以在你的例子中使用它:
std::vector<double> rainfall;
inputFile >> rainfall;
回答by pbn
Consider using this input reader:
考虑使用这个输入阅读器:
#include <iterator>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
template<typename T>
std::vector<T> parse_stream(std::istream &stream) {
std::vector<T> v;
std::istream_iterator<T> input(stream);
std::copy(input, std::istream_iterator<T>(), std::back_inserter(v));
return v;
}
int main(int argc, char* argv[])
{
std::ifstream input("/home/shared/data4.txt");
std::vector<int> v = parse_stream<int>(input);
for(auto &item: v) {
std::cout << item << std::endl;
}
return 0;
}
It allows you to use other stream types as well and is generic over type read.
它也允许您使用其他流类型,并且是类型读取的泛型。