<分区>
假设我有一个如下所示的文件:
1 2 6 2 3 7
3 7 1 2 3 7
在 C++ 中,如何将值存储在两个数组中,如下所示?
[1, 2, 6, 2, 3, 7]
[3, 7, 1, 2, 3, 7]
<分区>
假设我有一个如下所示的文件:
1 2 6 2 3 7
3 7 1 2 3 7
在 C++ 中,如何将值存储在两个数组中,如下所示?
[1, 2, 6, 2, 3, 7]
[3, 7, 1, 2, 3, 7]
最佳答案
使用两个 std::vector<int>
s 和一个 std::stringstream
:
std::vector<int> a, b;
std::string str1, str2;
if (std::getline(file, str1) && std::getline(file, str2))
{
std::stringstream iss(str1);
for (int n; iss >> n; )
a.push_back(n);
iss.clear();
iss.str(str2);
for (int n; iss >> n; )
b.push_back(n);
}
关于c++ - 从多行文本文件构建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439068/