我有一个包含数字的文件,例如字符串:
1 2
10 22
123 0
125 87
我想读成两个昏暗的数组,所以我有
lut[0]={1,2};
lut[1]={10,22};
lut[2]={123,0};
lut[3]={125,87};
用C ++最快的方法是什么
最佳答案
与处理器计算相比,任何文件输入/输出都比较慢。因此,您可以使用任何方式解析文件。运行时间将接近文件输入操作的时间。
代码样例
Ideone
#include <fstream>
const static int N = 1000;
int main()
{
int lut[N][2];
std::ifstream f("input_file.txt");
int index = 0;
while(!f.eof())
{
int l1 = 0;
int l2 = 0;
f >> l1 >> l2;
lut[index][0] = l1;
lut[index][1] = l2;
++index;
if (index == N)
break; // WARN
}
return 0;
}
关于c++ - 将包含int的字符串读取到数组的最快方法是什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16788454/