Results 1 to 6 of 6

Thread: problem with vector of vectors

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default problem with vector of vectors

    Qt Code:
    1. //my vector is: 700 vectors of 11 vectors of double
    2. vector < vector <double> > example;
    3. //fill example.........
    4. vector < vector <double> >* _set = &example;
    5.  
    6. vector < vector <double> >::iterator iit = _set->begin();
    7. cout << " iit size " << iit->size()<< endl; //print 11; but i need 700......how?
    8.  
    9. for (; iit != _set->end(); ++iit) {
    10. cout << (*iit)[0] << endl;
    11. (_set)->erase( (iit) );//doesn't work
    To copy to clipboard, switch view to plain text mode 

    I can't use this vector....I need to erase the first element (0) from the head of each 700 vectors. How can I do?
    Regards

  2. #2
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with vector of vectors

    Quote Originally Posted by mickey View Post
    Qt Code:
    1. //my vector is: 700 vectors of 11 vectors of double
    2. vector < vector <double> > example;
    To copy to clipboard, switch view to plain text mode 
    Are you sure? For me your vector is: 700 vectors of 11 doubles

    Quote Originally Posted by mickey View Post
    Qt Code:
    1. //fill example.........
    2. vector < vector <double> >* _set = &example;
    3. vector < vector <double> >::iterator iit = _set->begin();
    4. cout << " iit size " << iit->size()<< endl; //print 11; but i need 700......how?
    To copy to clipboard, switch view to plain text mode 
    iit is an iterator that points to the first element of you vector, so it points to vector of 11 doubles. That's why it prints 11. Use _set->size() and you'll get 700.

    Quote Originally Posted by mickey View Post
    Qt Code:
    1. for (; iit != _set->end(); ++iit) {
    2. cout << (*iit)[0] << endl;
    3. (_set)->erase( (iit) );//doesn't work
    To copy to clipboard, switch view to plain text mode 
    Here you're iterating through your vector (700 elements) and remove each element, so you probably end up with empty vector. Use sth like this:
    Qt Code:
    1. for (; iit != _set->end(); ++iit) {
    2. cout << (*iit)[0] << endl;
    3. iit->erase(iit->begin());
    4. }
    To copy to clipboard, switch view to plain text mode 
    and you should get vector which is: 700 vectors of 10 doubles.
    The Wheel weaves as the Wheel wills.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with vector of vectors

    Qt Code:
    1. vector < vector <double> > example;
    2. vector <double> line;
    3. line.push_back(......) // 11 times
    4. example.push_back(line); //700 times
    To copy to clipboard, switch view to plain text mode 
    hi,
    yours work! But I'm confused; Of course I wrote not right. But I think example is 1 vector of 700 vectors of double; why 700 vectors of double ?
    If I'm not doing a wrong how can I delete the first element of example ? (that's a vector of double)..
    thanks
    Regards

  4. #4
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with vector of vectors

    Quote Originally Posted by mickey View Post
    But I think example is 1 vector of 700 vectors of double; why 700 vectors of double ?
    When I've written "your vector is: 700 vectors of 11 doubles" I've meant "it contains", so it's actually the same you've written above: 1 vector of 700 vectors...
    Quote Originally Posted by mickey View Post
    If I'm not doing a wrong how can I delete the first element of example ? (that's a vector of double)..
    Qt Code:
    1. example.erase(example.begin());
    2. // or
    3. example.pop_front();
    To copy to clipboard, switch view to plain text mode 
    The Wheel weaves as the Wheel wills.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with vector of vectors

    Thank you, but is there a way to use only iterator? (eg. it is an iterator of example)
    Qt Code:
    1. exampe.erase (it.begin(); //or what?
    To copy to clipboard, switch view to plain text mode 
    and then: what happen to the memory? It'll have a blank space where it was the vector deleted?
    Regards

  6. #6
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with vector of vectors

    Google: "erase remove idiom".

    And don't worry too much about the memory, the vector will take care of the memory it uses (but only of the memory it uses, not of the memory pointers stored inside a vector point to).

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.