Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6560501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:28:06+00:00 2026-05-25T13:28:06+00:00

I’ve got a data file that is a single line consisting of a nested

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:28:06+00:00Added an answer on May 25, 2026 at 1:28 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.