On May 8, 12:33 am, "Default User" <defaultuse...@yahoo.comwrote:
Quote:
Jim Langston wrote:
Quote:
"barcaroller" <barcarol...@music.netwrote in message
news:f1o7ra$se3$1@aioe.org... >Is there a way of getting a C-style
pointer to the contents of a vector<T (similar to string's c_str)?
Quote:
Quote:
Quote:
>If not, what is the most efficent way of doing this? The reason I
need a C-style pointer is because I need to map the contents of the
vector<Tto a C-style struct (for reading, not writing).
Quote:
Quote:
A vector's data is guaranteed to be contiguous for just this reason.
One way is just to get the address of the first element.
Quote:
Which is more flexible than c_str(), for good or for bad. You can start
the pointer at any location in the vector, and it's not a const pointer
so you could change the vector contents if desired.
It's also a lot less transparent with regards to what you are
doing.
The next version of the C++ standard will make the same
guarantee for std::string, so &myString[0] will also be
guaranteed. In practice, it works with all implementations
today. The next version of the C++ standard will also add a
non-const function data() to std::string, and both const and
non-const versions of it to std::vector, so you can write what
you actually want, and not some not particularly transparent
work-around.
And of course, myString.data() + N allows geting a pointer to
any location of the string you want.
--
James Kanze (Gabi Software) email:
james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34