std::vector, std::copy and array of int

Discussion in 'C++' started by none, Apr 12, 2007.

  1. none

    none Guest

    Hi,
    Consider the following piece of code:
    int t[]={1,2,3,4,5,6};
    vector<int> v;
    std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));

    Could someone explain me why we can pass "t" as an argument to the copy
    method though it is expecting an iterator as its firts argument like
    written in the STL doc:

    iterator copy( iterator start, iterator end, iterator dest );

    Thanks,
    JF
     
    none, Apr 12, 2007
    #1
    1. Advertising

  2. On 12 Apr, 15:40, none <""jf\"@(none)"> wrote:
    > Hi,
    > Consider the following piece of code:
    > int t[]={1,2,3,4,5,6};
    > vector<int> v;
    > std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));
    >
    > Could someone explain me why we can pass "t" as an argument to the copy
    > method though it is expecting an iterator as its firts argument like
    > written in the STL doc:
    >
    > iterator copy( iterator start, iterator end, iterator dest );


    Because a pointer full fills all the requirements we have on an
    iterator in this case. Notice that iterator in this case is not a
    class but rather a template parameters, so anything having the right
    interface will do.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Apr 12, 2007
    #2
    1. Advertising

  3. none wrote:
    > Hi,
    > Consider the following piece of code:
    > int t[]={1,2,3,4,5,6};
    > vector<int> v;
    > std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));
    >
    > Could someone explain me why we can pass "t" as an argument to the copy
    > method though it is expecting an iterator as its firts argument like
    > written in the STL doc:
    >
    > iterator copy( iterator start, iterator end, iterator dest );

    ....

    That's not the signature for std::copy

    this is it:
    template<typename _InputIterator, typename _OutputIterator>
    inline _OutputIterator
    copy(_InputIterator __first, _InputIterator __last,
    _OutputIterator __result)

    Pointers behave like iterators and so you can use an pointer in place of
    any of the args in std::copy.

    Same applies to std::vector::append and one of the std::vector
    constructor overloads.
     
    Gianni Mariani, Apr 12, 2007
    #3
  4. none wrote:
    > int t[]={1,2,3,4,5,6};
    > vector<int> v;
    > std::copy (t, t+sizeof(t)/sizeof(t[0]), std::back_inserter (v));


    Unrelated to your question, but in case you didn't know, there's a
    shorter and more efficient way of doing that same thing:

    int t[]={1,2,3,4,5,6};
    vector<int> v(t, t+sizeof(t)/sizeof(t[0]));

    (This is more efficient because the vector can allocate the right
    amount of memory right away instead of having to resize itself
    repeatedly like when using the back inserter.)

    In case you can't call the vector's constructor like that (because
    you have to use a vector instance which has already been constructed
    elsewhere, or you simply have to perform that operation repeatedly),
    then use: v.assign(t, t+sizeof(t)/sizeof(t[0]));
     
    Juha Nieminen, Apr 14, 2007
    #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. Anonymous
    Replies:
    20
    Views:
    4,427
    Pete Becker
    Mar 30, 2005
  2. pmatos
    Replies:
    6
    Views:
    24,109
  3. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  5. Replies:
    3
    Views:
    3,220
Loading...

Share This Page