473,396 Members | 2,113 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.

vectors and arrays

Is there a way to easily convert between a std::vector and a C-style array?

In the Microsoft C++/Dinkumware library, std::vector::begin() returns a
pointer to the data type and not some custom iterator for all types except
bool. This pointer can then be used as the base pointer to the array.

I don't think this is standard behavior, and I believe the cast is strictly
incorrect according to the STL standard. Am I right, or can I expect certain
types such as std::vector<int> to be cast-able to an array?

-dr
Aug 28 '05 #1
3 1327
Dave Rahardja wrote:
Is there a way to easily convert between a std::vector and a C-style array?

In the Microsoft C++/Dinkumware library, std::vector::begin() returns a
pointer to the data type and not some custom iterator for all types except
bool. This pointer can then be used as the base pointer to the array.

I don't think this is standard behavior, and I believe the cast is strictly
incorrect according to the STL standard. Am I right, or can I expect certain
types such as std::vector<int> to be cast-able to an array?

-dr

assuming you have a prototype:

void f(T*, int nElems);

and a vector:

vector<T> v;

Then you can call:

f(&v[0],v.size());

Aug 28 '05 #2
"Dave Rahardja" <as*@me.com> wrote in message
news:ts********************************@4ax.com...
Is there a way to easily convert between a std::vector and a C-style
array? The elements of a vector are required to be stored contiguously,
and may therefore be accessed as an array.
For a **non-empty** vector v, a pointer to the first element can
be obtained with &v.front() , or &v[0] . or &*v.begin() .
NB: if v is empty, each of the above expressions will trigger
undefined behavior, so make sure to check for this case.
In the Microsoft C++/Dinkumware library, std::vector::begin() returns a
pointer to the data type and not some custom iterator for all types except
bool. This pointer can then be used as the base pointer to the array. This is an allowed behavior, but you cannot rely on it portably.
I don't think this is standard behavior, and I believe the cast is
strictly
incorrect according to the STL standard. Am I right, or can I expect
certain
types such as std::vector<int> to be cast-able to an array?

Equivalence of iterator and pointer is not portable.
Contiguous storage (and accessibility as a C array) is guaranteed.

hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 28 '05 #3
On Sun, 28 Aug 2005 10:39:57 +0200, "Ivan Vecerina"
<IN*************************@vecerina.com> wrote:

I don't think this is standard behavior, and I believe the cast is
strictly
incorrect according to the STL standard. Am I right, or can I expect
certain
types such as std::vector<int> to be cast-able to an array?

Equivalence of iterator and pointer is not portable.
Contiguous storage (and accessibility as a C array) is guaranteed.


I did not know that about contiguous storage. Thank you.

-dr
Aug 29 '05 #4

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

Similar topics

10
by: FLChamp | last post by:
SOrry if this message is a little confused, it most probably reflects the state of the author! I have made a small program that plots the orbit of a planet in visual python using visual.vector...
10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
2
by: J. Campbell | last post by:
I have a class that contains an array of integers that hold the state of my 'system'. The system must be updated periodically. I need to update the whole system at once, at which point the system...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
19
by: nick | last post by:
The attached program is fine, but I need to create vectors for each AcctExample object. I know that I can do the following: vector<AcctExample> example which makes a vector of AcctExample objects...
3
by: John Bend | last post by:
Hello. Can anyone please suggest some good tutorial links where Java Vectors and Arrays are explained and compared? I'm a proficient programmer but fairly new to Java. Whilst I understand...
7
by: Mack | last post by:
Hi Guys, In C++ , i was just wondering what is the advantage of Vectors over arrays? I saw one sample program doing pretty much same way traditional vectors do then what the use of VECTORS??or...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.