Array To Vector Conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Susman

    Array To Vector Conversion

    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.
  • Barry

    #2
    Re: Array To Vector Conversion

    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(Itera tor 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));

    Comment

    • Ambar Shukla

      #3
      Re: Array To Vector Conversion

      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" <derya.sus...@g mail.comwrote:
      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.

      Comment

      • Dave Rahardja

        #4
        Re: Array To Vector Conversion

        On 2007-12-25 05:45:21 -0600, "D. Susman" <derya.susman@g mail.comsaid:
        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

        Comment

        Working...