Array To Vector Conversion

Discussion in 'C++' started by D. Susman, Dec 25, 2007.

  1. D. Susman

    D. Susman Guest

    Hi,

    I am wondering if there is a short way (i.e. one liner) to convert an
    array into a list, vector etc. in c++.

    Thanks.
     
    D. Susman, Dec 25, 2007
    #1
    1. Advertising

  2. D. Susman

    Barry Guest

    D. Susman wrote:
    > Hi,
    >
    > I am wondering if there is a short way (i.e. one liner) to convert an
    > array into a list, vector etc. in c++.
    >


    STL Containers all have constructor

    Container(Iterator first, Iterator last, extra default parameters)

    take std::vector for example,
    you can write

    int arr[] = {1, 2, 3, 4, 5};
    std::vector<int> V(arr, arr + sizeof(arr)/sizeof(int));
     
    Barry, Dec 25, 2007
    #2
    1. Advertising

  3. D. Susman

    Ambar Shukla Guest

    Assuming
    - your vector is named a
    - the array is named a
    - the size of the array is s
    , one option would be:

    v.assign(a, a + s);

    Hope this helps.

    Cheers,
    Ambar Shukla.


    On Dec 25, 11:45 am, "D. Susman" <> wrote:
    > Hi,
    >
    > I am wondering if there is a short way (i.e. one liner) to convert an
    > array into a list, vector etc. in c++.
    >
    > Thanks.
     
    Ambar Shukla, Dec 25, 2007
    #3
  4. On 2007-12-25 05:45:21 -0600, "D. Susman" <> said:

    > Hi,
    >
    > I am wondering if there is a short way (i.e. one liner) to convert an
    > array into a list, vector etc. in c++.
    >
    > Thanks.


    You can also use boost::array, which provides STL iterator semantics to
    a plain old C array.

    -dr
     
    Dave Rahardja, Dec 25, 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. CD
    Replies:
    2
    Views:
    824
    Victor Bazarov
    Oct 5, 2004
  2. pmatos
    Replies:
    6
    Views:
    24,132
  3. Replies:
    8
    Views:
    2,001
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    624
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    398
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page