This vector assignment is too general, please help!

Original assignment :

Given a file with a random number of lines and each line consists of a random number of integers.

For example :

155 779 588
883 974 789 9533
345 534

Store all numbers in each line into a vector of integers. Then you print out the contents of the vectors.

Expected output :
Vector 1 :
155, 779, 588

Vector 2 :
883, 974, 789, 9533

Vector 3 :
345, 534

Another example :

255 999 755 143 633 247
884 335
993 566 100 633
124 755 332 624 825

Expected output :
Vector 1 :
255, 999, 755, 143, 633, 247

Vector 2 :
884, 335

Vector 3 :
993, 566, 100, 633

Vector 4 :
124, 755, 332, 624, 825

Please note that the number of lines may be unlimited and the number of integers for each line may be also unlimited.


I currently have no idea how to approach it. Could you please give me some general idea so that as to help me implement my solution? Thank you very much.
Last edited on
create a std::vector<std::vector<int>> as the holding container
read the file one line at a time with getline into a std::string object
create a std::istringstream object with the the std::string object from above
while reading the std::istringstream object into a dummy int push_back the int into a std::vector<int>
if the file is open push_back this std::vector<int> into the holding container
I have tried it, but I repeatedly fail and don't know where to start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main ()
{
stringstream ss;
string str;
vector < vector < int > > vect_numbers;
int numbers;
ifstream file_reader( "numbers.txt" );
if ( ! file_reader.is_open() )
{
cout << "Can't open file!\n";
}
else
{
while ( file_reader )
{
if ( ! getline( file_reader, str, '\n' ) )
{
break;
}
ss << str;
while ( ss >> numbers )
{
vect_numbers.resize( vect_numbers.size() + 1 );
vect_numbers.back().push_back( numbers );
ss.clear();
}
}
for ( int i = 0; i < vect_numbers.size(); i++ )
{
cout << "Vector " << i + 1 <<":\n";
for ( int j = 0; j < vect_numbers[ i ].size(); j++ )
{
cout << vect_numbers[ i ][ j ] << " ";
}
cout << endl;
}
}
file_reader.close();
cin.get();
}


With :
255 999 755 143 633 247
884 335
993 566 100 633
124 755 332 624 825

The program only outputs the first line, but each number is a vector! What is wrong here?
For each line you read you need to create another vector and insert it in the outer vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
  string str;
  vector < vector < int > > vect_numbers;
  int numbers;
  ifstream file_reader("numbers.txt");
  if (!file_reader.is_open())
  {
    cout << "Can't open file!\n";
  }
  else
  {
    while (getline(file_reader, str))
    {
      istringstream ss(str);
      vector<int> tmp;
      while (ss >> numbers)
      {
        tmp.push_back(numbers);
      }
      vect_numbers.push_back(tmp);
    }
    int i = 0;
    for (auto v: vect_numbers)
    {
      cout << "Vector " << i + 1 << ":\n";
      for (auto num : v)
      {
        cout << num << "\t";
      }
      cout << "\n";
      i++;
    }
  }
  cin.get();
}



OUTPUT:

Vector 1:
255 999 755 143 633 247
Vector 2:
884 335
Vector 3:
993 566 100 633
Vector 4:
124 755 332 624 825

if the file is open push_back this std::vector<int> into the holding container

I'd probably stick in a if(file_reader){} loop around vect_numbers.push_back(tmp) to make sure that we get predictable results (cf: discussion with Chervil on this couple days back: http://www.cplusplus.com/forum/beginner/206784/)
And std::string str might well be declared within the else loop in keeping with the principle of declaring variables as locally scoped as possible: http://stackoverflow.com/questions/3773396/declare-variables-at-top-of-function-or-in-separate-scopes
Thanks all. Because of all of you I was able to complete this assignment :)
Topic archived. No new replies allowed.