![](/img/trans.png)
[英]how to read data delimited by space and save it to arrays,and then write it other order in new text file in c++
[英]How to read in space-delimited information from a file in c++
在文本文件中,我将有一行包含一系列数字,每个数字用空格分隔。 我如何阅读这些数字并将所有数字存储在一个数组中?
std::ifstream file("filename");
std::vector<int> array;
int number;
while(file >> number) {
array.push_back(number);
}
只需将它们从流中复制到数组:
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::ifstream file("filename");
std::vector<int> array;
std::copy( std::istream_iterator<int>(file),
std::istream_iterator<int>(),
std::back_inserter(array));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.