Pointer to vector<T> contents

B

barcaroller

Is there a way of getting a C-style pointer to the contents of a vector<T>
(similar to string's c_str)?

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<T> to a
C-style struct (for reading, not writing).
 
J

Jim Langston

barcaroller said:
Is there a way of getting a C-style pointer to the contents of a vector<T>
(similar to string's c_str)?

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<T> to
a C-style struct (for reading, not writing).

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.

&MyVector[0]
 
R

red floyd

barcaroller said:
Is there a way of getting a C-style pointer to the contents of a vector<T>
(similar to string's c_str)?

&v[0] // precondition -- v is not empty.
 
D

Default User

Jim said:
>Is there a way of getting a C-style
pointer to the contents of a vector said:
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<T> to a C-style struct (for reading, not writing).

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.

&MyVector[0]

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.




Brian
 
J

James Kanze

Jim said:
>Is there a way of getting a C-style
pointer to the contents of a vector<T> (similar to string's c_str)?
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<T> to a C-style struct (for reading, not writing).
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.
&MyVector[0]
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.
 
D

Default User

James said:
Jim said:
>Is there a way of getting a
C-style pointer to the contents of a vector<T> (similar to
string's c_str)?
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<T> to a C-style struct (for reading, not writing).
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.
&MyVector[0]
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.

And fraught with the usual perils of raw access.
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.

I was never sure if original committee thought there would be some
esoteric implementations of std::string or what. The most obvious
implementation is a contiguous buffer of sufficient size to hold
length+1, and return a pointer to the start for c_str().
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.

There are times when such access can be useful when dealing with legacy
routines. I'd have some concern about people using it inappropriately,
but those who want to would find ways regardless.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top