# insert in a vector of vector

• 5th March 2007, 23:07
mickey
insert in a vector of vector
hi I'm trying to do this:
Code:

`vector < vector <double> > temp; temp.resize(_set->size()); vector < vector <double> >::iterator iit=temp.begin(); int i=0; vector < vector <double> >::iterator itra = _set->begin(); srand ((unsigned)time(NULL)); while ( ! _set->empty() ) { do { i = rand() % (int) _set->size(); cout << " i " << i << endl; if (i == 0 && _set->size() == 1) break; } while (i >= (int) _set->size() || i == 0); temp.insert (temp.begin(), itra+i, itra+i); //here it is filling nothing int dist = (int) distance (_set->begin(), _set->begin()+i); _set->erase(itra+dist); ++iit; } *_set = temp;`
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
• 5th March 2007, 23:13
wysota
Re: insert in a vector of vector
Can't you use std::shuffle?
• 5th March 2007, 23:36
mickey
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?)
• 6th March 2007, 08:45
Re: insert in a vector of vector
Quote:

Code:

`vector < vector <double> > temp;temp.resize(_set->size()); vector < vector <double> >::iterator iit=temp.begin();`

What is this iterator for? You only increase it in your loop, but you don't use it anywhere.
Quote:

Code:

`int i=0;vector < vector <double> >::iterator itra = _set->begin(); srand ((unsigned)time(NULL));while ( ! _set->empty() ) { do { i = rand() % (int) _set->size(); cout << " i " << i << endl; if (i == 0 && _set->size() == 1) break; } while (i >= (int) _set->size() || i == 0);`

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().
Quote:

Code:

`temp.insert (temp.begin(), itra+i, itra+i); //here it is filling nothing`

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.