473,395 Members | 1,401 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,395 software developers and data experts.

Vector as array

Let v be an vector; for instance:
std::vector<doublev;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?

Alex Vinokur

Sep 21 '08 #1
3 1292
Alex Vinokur wrote:
Let v be an vector; for instance:
std::vector<doublev;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?
I think that is what the standard means by calling the memory of a
vector "contiguous". The precise guarantee is for types other that bool
[23.2.4/1]:

... The elements of a vector are stored contiguously, meaning that if v is
a vector<T, Allocatorwhere T is some type other than bool, then it obeys
the identity &v[n] == &v[0] + n for all 0 <= n < v.size().
Best

Kai-Uwe Bux
Sep 21 '08 #2
Alex Vinokur wrote:
Let v be an vector; for instance:
std::vector<doublev;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?
It is, except when 'v.size() == 0', in which case 'v[0]' is
undefined (and produces runt-time errors on some popular
platforms).
Alex Vinokur
Schobi
Sep 21 '08 #3
Alex Vinokur wrote:
Let v be an vector; for instance:
std::vector<doublev;

Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?
Yes.
In other words,
Let foo() have the following prototype:
void (double* p, std::size_t arrarySize);

Is this calling is correct:
foo (&v[0], v.size()) ?
Yes.
Sep 21 '08 #4

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...
9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
19
by: Carlo Milanesi | last post by:
Mathematically speaking, a 'vector' is something you can add to another vector and multiply by a number. But in C++, the following code is illegal: std::vector<double> v1(3), v2(3); v1 + v2; //...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
19
by: Matteo Migliore | last post by:
Hi! I've to scale a vector of numbers of size N to a vector of size M. The trasformation is like a zoom on images but I need on a vector. The second size can be M >= N or M <= N, M 0. The...
29
by: stephen b | last post by:
Hi all, personally I'd love to be able to do something like this: vector<intv; v.assign(1, 2, 5, 9, 8, 7) etc without having to manually add elements by doing v = 1, v = 2 .. etc. it would...
6
by: remlostime | last post by:
now, i write some code code1: int a; int main(){} code2: vector<inta(10000000); int main(){} after using g++ compile, and run it, code1 is broken, but code2 runs
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.