std::vector
is guaranteed to store its elements in contiguous memory, so what you are attempting to do is OK, provided that myClassType
is a POD type. Otherwise, you will have to loop through the std::vector
serializing each element one at a time to a flat format as needed.
However, the std::vector::begin()
method returns an iterator to the first element, not a pointer to the element data. An iterator is like a pointer, but is not a pointer. Container implementations are allowed to use actual pointers as iterators, but more times than not they use wrapper classes instead. Do not rely on this behavior! Use the iterator API the way it is intended to be used, and do not assume an iterator is an actual pointer.
To get a valid pointer to the data of the first element of your vector, you need to either:
use std::vector::data()
(C++11 and later only):
myVect.data()
This is safe to use even if the vector is empty. data()
may or may not return nullptr
when size()
is 0, but that is OK since write()
will not try to access any memory when size()
is 0.
bool OK = File.write((char*) myVect.data(), myVect.size() * sizeof(myClassType));
use std::vector::operator[]
to get a reference to the first element, then use operator&
to get the element's address:
&myVect[0]
Note that operator[]
does not perform bounds checking, and thus will return an undefined pointer if the specified index is >= size()
, so make sure to check if the vector is empty before writing it:
bool OK = myVect.empty() || File.write((char*) &myVect[0], myVect.size() * sizeof(myClassType));
same as #2, but using std::vector::front()
instead:
&myVect.front()
Note that calling front()
on an empty vector is undefined behavior, so make sure to check if the vector is empty:
bool OK = myVect.empty() || File.write((char*) &myVect.front(), myVect.size() * sizeof(myClassType));
use begin()
to get an iterator to the first element, then use operator*
to dereference the iterator to get a reference to the element it points to, then you can get the element's address:
&(*(myVect.begin()))
Note that begin()
returns the end()
iterator when the vector is empty, and dereferencing the end()
iterator is undefined behavior, so make sure to check if the vector is empty:
bool OK = myVect.empty() || File.write((char*) &(*(myVect.begin())), myVect.size() * sizeof(myClassType));