Raw access to vector elements

O

Ole Nielsby

I need to build a small array of pointers to a type MyType.
The array size can be from 1 upwards, is not known
beforehand, typically < 10 but I don't want a small
fixed limit. Elements will be sorted by pointer address
except the first one which is special.

I can declare

std::vector<MyType*> builder;

and do searches and insertions on it. But when I am done,
I need an array pointer. I assume the vector stores its elements
in an array, replacing it by a larger one by demand, and the
iterators are, behind the cover, plain pointers into this array.

I need to pass a pointer to this block to a proc that will do
some hashing and copying from it, and I'd rather pass it
directly than having to copy it.

The compiler (VC2005) accepts this:

MyType **peek = &*builder.begin();

but does this always mean peek[0] ... peek[n-1] are the
vector elements, or could the vector arrange them
differently?

(In case you wonder what it's for: I am writing a pattern
compiler and the vector is used to hold the names of the
free variables in the pattern. Patterns will be generated
and compiled dynamically, so the pattern compiler must
be fast. Since the number of free variables in a pattern
is usually quite small, using a std::set would be overkill.)
 
I

Ian Collins

Ole said:
I need to build a small array of pointers to a type MyType.
The array size can be from 1 upwards, is not known
beforehand, typically < 10 but I don't want a small
fixed limit. Elements will be sorted by pointer address
except the first one which is special.

I can declare

std::vector<MyType*> builder;

and do searches and insertions on it. But when I am done,
I need an array pointer. I assume the vector stores its elements
in an array, replacing it by a larger one by demand, and the
iterators are, behind the cover, plain pointers into this array.

I need to pass a pointer to this block to a proc that will do
some hashing and copying from it, and I'd rather pass it
directly than having to copy it.

The compiler (VC2005) accepts this:

MyType **peek = &*builder.begin();
The standard way of doing this is

MyType **peek = &builder[0];
 
T

Thomas Tutone

Ole said:
I need to build a small array of pointers to a type MyType.
The array size can be from 1 upwards, is not known
beforehand, typically < 10 but I don't want a small
fixed limit. Elements will be sorted by pointer address
except the first one which is special.

I can declare

std::vector<MyType*> builder;

and do searches and insertions on it. But when I am done,
I need an array pointer. I assume the vector stores its elements
in an array, replacing it by a larger one by demand, and the
iterators are, behind the cover, plain pointers into this array.

Not necessarily. In some implementations, vector iterators are
pointers. They are not in many others, including gcc's implementation.
In any case, a vector is not stored in a C-style array, but it is
stored in a contiguous block of memory, just like an array.
I need to pass a pointer to this block to a proc that will do
some hashing and copying from it, and I'd rather pass it
directly than having to copy it.

The compiler (VC2005) accepts this:

MyType **peek = &*builder.begin();

That's fine. You could also use &builder.front(). In either case,
just make sure that
!builder.empty().
but does this always mean peek[0] ... peek[n-1] are the
vector elements, or could the vector arrange them
differently?

It's guaranteed that the vector storage will be contiguous, like a
C-style array, so you should be fine.

Best regards,

Tom
 
R

red floyd

Ole said:
I need to pass a pointer to this block to a proc that will do
some hashing and copying from it, and I'd rather pass it
directly than having to copy it.

The compiler (VC2005) accepts this:

MyType **peek = &*builder.begin();

but does this always mean peek[0] ... peek[n-1] are the
vector elements, or could the vector arrange them
differently?

No, vector is guaranteed to store its elements contiguously.
An alternative (and perhaps a bit clearer) notation would be:

MyType** peed = &builder[0];
 
O

Ole Nielsby

Thomas said:
It's guaranteed that the vector storage will be contiguous, like a
C-style array, so you should be fine.

Thanks. This was what I needed to know.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top