So assume I have a file as shown below:
1 2 6 2 3 7
3 7 1 2 3 7
In C++, how can I store the values in two arrays like the ones below?
[1, 2, 6, 2, 3, 7]
[3, 7, 1, 2, 3, 7]
So assume I have a file as shown below:
1 2 6 2 3 7
3 7 1 2 3 7
In C++, how can I store the values in two arrays like the ones below?
[1, 2, 6, 2, 3, 7]
[3, 7, 1, 2, 3, 7]
Use two std::vector<int>
s and a 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);
}
Take a look boost::tokenizer
and, like a comment said, use std::vector
.