Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Array To Vector Conversion

Reply
Thread Tools

Array To Vector Conversion

 
 
D. Susman
Guest
Posts: n/a
 
      12-25-2007
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.
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      12-25-2007
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));
 
Reply With Quote
 
 
 
 
Ambar Shukla
Guest
Posts: n/a
 
      12-25-2007
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.com> 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.


 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      12-25-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Uniform vector class, inheriting from Array: How to make sure thatmethods return a Vector and not an Array? Thomas Ruby 7 05-23-2005 04:21 PM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57