Vector as array

A

Alex Vinokur

Let v be an vector; for instance:
std::vector<double> v;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?

Alex Vinokur
 
K

Kai-Uwe Bux

Alex said:
Let v be an vector; for instance:
std::vector<double> v;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?

I think that is what the standard means by calling the memory of a
vector "contiguous". The precise guarantee is for types other that bool
[23.2.4/1]:

... The elements of a vector are stored contiguously, meaning that if v is
a vector<T, Allocator> where T is some type other than bool, then it obeys
the identity &v[n] == &v[0] + n for all 0 <= n < v.size().


Best

Kai-Uwe Bux
 
R

Rolf Magnus

Alex said:
Let v be an vector; for instance:
std::vector<double> v;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?
Yes.

In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?

Yes.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top