473,804 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<MyT ype*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 2852
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<MyT ype*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<MyT ype*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
19610
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 grow a vector it may copy all of its elements to different memory location to accomodate the new vector size. Is it possible that at some point an element of fixed array e.g. a_vStr gets invalid or
5
5665
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 for(int i = 0; i < 10; ++i) { #5 *ii++ = 4; #6 } #7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
11
8884
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 perfomance than if I first define the vector of a given size, then write to the elements with myvec = However, I'm currently thinking that it isn't feasible to obtain the vector size, so really need to resize the vector dynamically as I go. Is...
11
2907
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 circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
6
3129
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
4183
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
11
2750
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
2021
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 access the elements of that list. I have a list of vectors with 4 elements. The size of my list is 500, the size of every vector is 4.
3
6736
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 correct. I'm trying to create a data structure that will allow me to have a dynamic number of columns and a dynamic number of rows in each column (there may be ten rows in column one and twenty rows in column two for example). i. I can declare...
0
9577
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10569
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.