Pointer to a vector
-
Hello, I previously wrote the contents of an array of floats to a file, in binary, using the following; OutputFile.write((char*)Z, sizeof(float) * n); //write n elements of array Z Instead of an array, if Z is defined as a Vector of floats, is there an equivalent of the above. Can I obtain a pointer to the start of the vector? Thanks, John
-
Hello, I previously wrote the contents of an array of floats to a file, in binary, using the following; OutputFile.write((char*)Z, sizeof(float) * n); //write n elements of array Z Instead of an array, if Z is defined as a Vector of floats, is there an equivalent of the above. Can I obtain a pointer to the start of the vector? Thanks, John
Think I've got the answer - dereference the vector... (char*) &Z
-
Hello, I previously wrote the contents of an array of floats to a file, in binary, using the following; OutputFile.write((char*)Z, sizeof(float) * n); //write n elements of array Z Instead of an array, if Z is defined as a Vector of floats, is there an equivalent of the above. Can I obtain a pointer to the start of the vector? Thanks, John
If
v
is the name of the vector, then&v[0]
points to the underlying buffer. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Think I've got the answer - dereference the vector... (char*) &Z
John Oliver wrote: Think I've got the answer - dereference the vector... (char*) &Z That will be wrong. You have to dereference the first element like &Z[0]. ..this is a VB Programmers' world and we all are just visitors - Someone in a MSJ article
-
If
v
is the name of the vector, then&v[0]
points to the underlying buffer. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
what's the difference between a vector and a one-dimensional array in C++? as far as i know, nothing. so, this is an equivalence class: &V[0] &(*(V+0)) &(*(V)) V therefore, you can use V if you want.
The difference is that an array is a wrapper around the data, therefore, the correct answer remains &v[0];. A vector of int's is a block of ints, the first one being at &v[0];. v has nothing to do with int's or memory, it is an instance of the vector class. In fact, if you want a pointer to an element, you must dereference an iterator, then ask for the address, exactly as you have put it: &(*(V)). VC6 wrongly will accept the iterator as a pointer, VC7 corrects this. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002