0

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]
Vedaad Shakib
  • 739
  • 7
  • 20

2 Answers2

1

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);
}
David G
  • 94,763
  • 41
  • 167
  • 253
0

Take a look boost::tokenizer and, like a comment said, use std::vector.

Aaron D. Marasco
  • 6,506
  • 3
  • 26
  • 39