Hooked Hooked - 2 months ago 12
C++ Question

Overload stream operator for nested template C++ STL

I've got a data file that is a single line consisting of a nested series of doubles eg.

[[0.127279,0.763675,0.636396],[0.254558,0.890955,0.636396],
[0.127279,0.636396,0.763675],[0.254558,0.763675,0.763675],
[0.381838,0.890955,0.763675],[0.127279,0.509117,0.890955],
[0.254558,0.636396,0.890955],[0.509117,0.890955,0.890955]]


I'd like to be able to read this into a STL
vector<vector<double> >
using the stream operator which is templated across the inner type of A:

vector<vector<double> > A;
FIN >> A;


I've figured out a way to do this when the vector is not nested, ie. a simple
vector<T>
as so:

template <class T>
istream& operator>>(istream& s, vector<T> &A){
T x;
string token; char blank;

s >> blank; // Gobble the first '['
while( getline(s, token, ',') ) {
istringstream input(token);
input >> x;
A.push_back(x);
}
s >> blank; // Gobble the last ']'
return s;
}


But I'm having problem with the
istream& operator>>(istream& s, vector<vector<T> >&A)
part as I can't seem to catch the inner
]
's properly. I'm sure that Boost has a way of doing this, but I'd like to see a solution with the STL for pedagogical purposes.

Note: I'm aware that overloading the stream operator for
vector<T>
can have far-reaching undesirable consequences and that the implementation should be wrapped up in its own class - I'm using this example above as it stands to clarify the question.

EDIT:
I'd like the method to be robust enough to handle a input array whose size (and inner array) size is not known in advance, but inferred from reading the stream.

Answer

Actually, the problem with your code that you want to use the same function for both, when T is:

  • vector<double>
  • double

But the logic which needs to read the data into vector and double is slightly different. So you cannot do that, at least not with that simple logic:

I would prefer to write two functions, to handle both cases separately. After all, even in your case, the compiler will generate two different functions for each value of T.

template <class T>
istream& operator>>(istream& s, vector<T> &A)
{
  T x;
  string token; char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ',') ) 
  {
   istringstream input(token);
   input >> x;
   A.push_back(x);
  }
// s >> blank; // Gobble the last ']'
  return s;
}

template <class T>
istream& operator>>(istream& s, vector<vector<T>> &A)
{
  vector<T> x;
  string token; 
  char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ']') ) 
  {
   istringstream input(token);
   input >> x;
   s >> blank; //read , after [...]
   A.push_back(x);
   x.clear();
  }
  s >> blank; // Gobble the last ']'
  return s;
}

Test code:

int main() {
        vector<vector<double>> A;       
        cin >> A;
        for(size_t i = 0 ;i < A.size(); ++i)
        {
            for(size_t j = 0 ; j < A[i].size(); ++j)
                 cout << A[i][j] <<"   ";
            cout << endl;
        }
        return 0;
}

Input:

[[1,2,3],[4,5,6],
[7,8,9],[10,11,12],
[13,14,15],[16,17,18],
[19,20,21],[22,23,24]]

Output:

1   2   3   
4   5   6   
7   8   9   
10   11   12   
13   14   15   
16   17   18   
19   20   21   
22   23   24 

Online demo : http://ideone.com/iBbmw

Comments