473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Raw access to vector elements

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.)
Oct 5 '06 #1
4 2829
Ole Nielsby wrote:
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];

--
Ian Collins.
Oct 6 '06 #2
Ole Nielsby wrote:
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

Oct 6 '06 #3
Ole Nielsby wrote:
>

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];
Oct 6 '06 #4
Thomas Tutone wrote:
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.
Oct 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we...
5
by: john smith | last post by:
HI, when I try the following code, I get a segfault when compiled with VC.NET and g++ under cygwin. #1 vector<int> vi; #2 vector<int>::iterator ii = vi.begin(); #3 vi.reserve(10); #4 ...
11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
2
by: steflhermitte | last post by:
Dear cpp-ians, I have created a list of vectors and its iterator: list < vector <long double> > listValues; list < vector <long double> >::iterator itListValues; but I don't know how to...
3
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.