Velocity Reviews > C++ > Vector as array

# Vector as array

Alex Vinokur
Guest
Posts: n/a

 09-21-2008
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

Kai-Uwe Bux
Guest
Posts: n/a

 09-21-2008
Alex Vinokur wrote:

> 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

Rolf Magnus
Guest
Posts: n/a

 09-21-2008
Alex Vinokur wrote:

> 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.