468,513 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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.
Dec 25 '07 #1
3 6466
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<intV(arr, arr + sizeof(arr)/sizeof(int));
Dec 25 '07 #2
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...@gmail.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.
Dec 25 '07 #3
On 2007-12-25 05:45:21 -0600, "D. Susman" <de**********@gmail.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

Dec 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jeff Williams | last post: by
1 post views Thread by Ruediger Knoerig | last post: by
2 posts views Thread by James | last post: by
30 posts views Thread by James Daughtry | last post: by
32 posts views Thread by Mateo | last post: by
11 posts views Thread by Sudzzz | last post: by
27 posts views Thread by mike3 | last post: by
9 posts views Thread by Slain | last post: by
4 posts views Thread by Edward Jensen | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.