View Full Version : problem with vector of vectors

mickey

14th February 2007, 10:41

//my vector is: 700 vectors of 11 vectors of double

vector < vector <double> > example;

//fill example.........

vector < vector <double> >* _set = &example;

vector < vector <double> >::iterator iit = _set->begin();

cout << " iit size " << iit->size()<< endl; //print 11; but i need 700......how?

for (; iit != _set->end(); ++iit) {

cout << (*iit)[0] << endl;

(_set)->erase( (iit) );//doesn't work

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?

danadam

14th February 2007, 11:56

//my vector is: 700 vectors of 11 vectors of double

vector < vector <double> > example;

Are you sure? For me your vector is: 700 vectors of 11 doubles

//fill example.........

vector < vector <double> >* _set = &example;

vector < vector <double> >::iterator iit = _set->begin();

cout << " iit size " << iit->size()<< endl; //print 11; but i need 700......how?

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.

for (; iit != _set->end(); ++iit) {

cout << (*iit)[0] << endl;

(_set)->erase( (iit) );//doesn't work

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:

for (; iit != _set->end(); ++iit) {

cout << (*iit)[0] << endl;

iit->erase(iit->begin());

}

and you should get vector which is: 700 vectors of 10 doubles.

mickey

14th February 2007, 12:44

vector < vector <double> > example;

vector <double> line;

line.push_back(......) // 11 times

example.push_back(line); //700 times

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

danadam

14th February 2007, 13:01

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...

If I'm not doing a wrong how can I delete the first element of example ? (that's a vector of double)..

example.erase(example.begin());

// or

example.pop_front();

mickey

14th February 2007, 13:08

Thank you, but is there a way to use only iterator? (eg. it is an iterator of example)

exampe.erase (it.begin(); //or what?

and then: what happen to the memory? It'll have a blank space where it was the vector deleted?

Methedrine

14th February 2007, 13:23

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).

Powered by vBulletin® Version 4.2.5 Copyright © 2020 vBulletin Solutions Inc. All rights reserved.