valarrays and regular arrays

  • Thread starter Sebastian Thiebes
  • Start date
S

Sebastian Thiebes

Hello all,

I am using valarrays of double in my code, but for using certain
functions I need to pass a normal array of double. What I did was
write something like this:

valarray<double> vad;
// somehow fill this valarray
double* da = new double[vad.size()];

for (long i = 0; i < vad.size(); ++i)
{
da = vad;
}

This works fine. However I feel this not to be an elegant solution to
the proplem, is there a way to maybe directly cast a valarray into a
normal array, or is there another way to do this conversion more
efficiently?

Thanks in advance,

Sebastian
 
J

John Harrison

Sebastian Thiebes said:
Hello all,

I am using valarrays of double in my code, but for using certain
functions I need to pass a normal array of double. What I did was
write something like this:

valarray<double> vad;
// somehow fill this valarray
double* da = new double[vad.size()];

for (long i = 0; i < vad.size(); ++i)
{
da = vad;
}

This works fine. However I feel this not to be an elegant solution to
the proplem, is there a way to maybe directly cast a valarray into a
normal array, or is there another way to do this conversion more
efficiently?

Thanks in advance,

Sebastian


The simple &vad[0] does the trick.

some_function(&vad[0]);

john
 
S

Sebastian Thiebes

John said:
The simple &vad[0] does the trick.

some_function(&vad[0]);

Thanks a lot! This is what I was hoping for,
a simple and elegant solution.

Seb
 
M

Marc Schellens

Sebastian said:
Hello all,

I am using valarrays of double in my code, but for using certain
functions I need to pass a normal array of double. What I did was
write something like this:

valarray<double> vad;
// somehow fill this valarray
double* da = new double[vad.size()];

for (long i = 0; i < vad.size(); ++i)
{
da = vad;
}

This works fine. However I feel this not to be an elegant solution to
the proplem, is there a way to maybe directly cast a valarray into a
normal array, or is there another way to do this conversion more
efficiently?

Thanks in advance,

Sebastian


You could do:

double* da = &vad[0];

HDH,
marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top