How to use std::copy?

Discussion in 'C++' started by kathy, Jan 24, 2006.

  1. kathy

    kathy Guest

    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);
     
    kathy, Jan 24, 2006
    #1
    1. Advertising

  2. kathy

    Guest

    kathy wrote:
    > 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);
     
    , Jan 24, 2006
    #2
    1. Advertising

  3. kathy wrote:
    > 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
     
    Victor Bazarov, Jan 24, 2006
    #3
  4. kathy

    Cy Edmunds Guest

    "kathy" <> wrote in message
    news:...
    >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).
     
    Cy Edmunds, Jan 25, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,330
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    368
    Jim Langston
    Jul 7, 2005
  3. Vinu
    Replies:
    0
    Views:
    358
  4. Geoffrey S. Knauth
    Replies:
    6
    Views:
    1,008
    Earl Purple
    Jan 18, 2006
  5. Siegfried Heintze

    How to use std::copy with std::map?

    Siegfried Heintze, May 3, 2006, in forum: C++
    Replies:
    1
    Views:
    881
    Victor Bazarov
    May 4, 2006
Loading...

Share This Page