PDA

View Full Version : question on vector an new



mickey
6th July 2006, 17:47
hi, I need to obtain a vector of arrays;
Is this correct? thanks


GLfloat* position = new GLfloat[4];
vector <GLfloat*> ppp;
for (int g=0; g < ....; g++) {
for (int i=0; i<4; i++) {
position[i] = ch.toInt(); // split a string as "1543"
}

ppp.push_back(position);
}

for( std::vector<GLfloat*>::iterator i = ppp.begin(); i != ppp.end(); ++i ) {
delete [] *i;
}
ppp.clear();

jacek
6th July 2006, 18:00
No, because you create and change only one array. In result ppp contains a number of pointers to that single array.

mickey
6th July 2006, 18:16
Right?


vector <GLfloat*> ppp;
for (int g=0; g < ....; g++) {
GLfloat* position = new GLfloat[4];
for (int i=0; i<4; i++) {
position[i] = ch.toInt(); // split a string as "1543"
}
ppp.push_back(position);
}

for( std::vector<GLfloat*>::iterator i = ppp.begin(); i != ppp.end(); ++i ) {
delete [] *i;
}
ppp.clear();

and how obtain value of fist element array of first element vector?? thanks

jacek
6th July 2006, 18:29
Right?
Now it looks OK.


and how obtain value of fist element array of first element vector?? thanks

GLfloat f = ppp[0][0];
// or
GLfloat f = ppp.at( 0 )[ 0 ];
http://www.cppreference.com/cppvector/at.html