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