Array To Vector Conversion

D

D. Susman

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.
 
B

Barry

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++.

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));
 
A

Ambar Shukla

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.
 
D

Dave Rahardja

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top