How to use std::copy?

K

kathy

I have array:

double a[1024];
double b[1024];
std::vector <double> vDouble;

when I use:

std::copy(a,a+1024,vDouble.begin());

I got error.

How to use std::copy?

Also, Is the following usage correct?
std::copy(a,a+1024,b);
 
R

roberts.noah

kathy said:
I have array:

double a[1024];
double b[1024];
std::vector <double> vDouble;

when I use:

std::copy(a,a+1024,back_inserter(vDouble));

I got error.

How to use std::copy?

Also, Is the following usage correct?
std::copy(a,a+1024,b);
 
V

Victor Bazarov

kathy said:
I have array:

double a[1024];
double b[1024];
std::vector <double> vDouble;

when I use:

std::copy(a,a+1024,vDouble.begin());

I got error.

Yes. Your vector is empty. You need to either resize it to 1024 elements
or copy to a 'back_inserter(vDouble)'.
How to use std::copy?

Also, Is the following usage correct?
std::copy(a,a+1024,b);

Yes.

V
 
C

Cy Edmunds

kathy said:
I have array:

double a[1024];
double b[1024];
std::vector <double> vDouble;

when I use:

std::copy(a,a+1024,vDouble.begin());

I got error.

How to use std::copy?

Also, Is the following usage correct?
std::copy(a,a+1024,b);

Victor's answer is correct of course, but actually this isn't a great usage
of std::copy. It is probably better to declare vDouble as

std::vector<double> vDouble(a, a+1024);

If your standard library implementation is any good this should prevent
anything ugly from happening (such as initializing all 1024 elements to 0.0
and then overwriting them immediately or doing all the bookkeeping for 1024
push_back operations).
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top