Results 1 to 4 of 4

Thread: insert in a vector of vector

Hybrid View

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

    Default insert in a vector of vector

    hi I'm trying to do this:
    Qt Code:
    1. vector < vector <double> > temp;
    2. temp.resize(_set->size());
    3. vector < vector <double> >::iterator iit=temp.begin();
    4. int i=0;
    5. vector < vector <double> >::iterator itra = _set->begin();
    6. srand ((unsigned)time(NULL));
    7. while ( ! _set->empty() ) {
    8. do {
    9. i = rand() % (int) _set->size();
    10. cout << " i " << i << endl;
    11. if (i == 0 && _set->size() == 1) break;
    12. } while (i >= (int) _set->size() || i == 0);
    13. temp.insert (temp.begin(), itra+i, itra+i); //here it is filling nothing
    14. int dist = (int) distance (_set->begin(), _set->begin()+i);
    15. _set->erase(itra+dist);
    16. ++iit;
    17. }
    18. *_set = temp;
    To copy to clipboard, switch view to plain text mode 
    my aim is take my vec of vec and shuffle its elements (its vectors).
    Before chose a vector of _set at random, put it at begin of temp and erase it from _set; I've been trying this way and it seems me more difficult...
    the calcolus of 'i' is ok. but insert doesn't work.
    Can anyone help me?
    thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: insert in a vector of vector

    Can't you use std::shuffle?

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

    Default Re: insert in a vector of vector

    I didn't know it; thanks. But some hints to understand the wrong behavoir of my code ? (ie why insert don't work?)
    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: insert in a vector of vector

    Qt Code:
    1. vector < vector <double> > temp;
    2. temp.resize(_set->size());
    3. vector < vector <double> >::iterator iit=temp.begin();
    To copy to clipboard, switch view to plain text mode 
    What is this iterator for? You only increase it in your loop, but you don't use it anywhere.
    Qt Code:
    1. int i=0;
    2. vector < vector <double> >::iterator itra = _set->begin();
    3. srand ((unsigned)time(NULL));
    4. while ( ! _set->empty() ) {
    5. do {
    6. i = rand() % (int) _set->size();
    7. cout << " i " << i << endl;
    8. if (i == 0 && _set->size() == 1) break;
    9. } while (i >= (int) _set->size() || i == 0);
    To copy to clipboard, switch view to plain text mode 
    These conditions are redundant, I guess. By using % operator variable "i" takes value from interval 0 (incl), set->size()(excl). So if size is 1 then it's no use checking if "i" is zero. Additionally variable "i" will never be greater or equal then set->size().
    Qt Code:
    1. temp.insert (temp.begin(), itra+i, itra+i); //here it is filling nothing
    To copy to clipboard, switch view to plain text mode 
    These two iterators points the same element. insert() method insert sequence from first(incl) to last(excl), so in your case it inserts nothing. Try to pass the last argument as itra+i+1.
    The Wheel weaves as the Wheel wills.

Similar Threads

  1. [Qt4.1] How to insert an image inside a Form?
    By Gonzalez in forum Qt Tools
    Replies: 5
    Last Post: 23rd September 2008, 12:20
  2. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 04:56
  3. problem with vector of vectors
    By mickey in forum General Programming
    Replies: 5
    Last Post: 14th February 2007, 14:23
  4. vector of vector size
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th February 2007, 16:59
  5. vector of objects
    By mickey in forum General Programming
    Replies: 2
    Last Post: 8th May 2006, 21:13

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.