473,378 Members | 1,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 6811
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) {...
1
by: Ruediger Knoerig | last post by:
In my current project I used templates to provide an universial interface to vector objects. For this purpose I declared two methods with an additional template argument: template<class Vector>...
2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
32
by: Mateo | last post by:
I have char *array and it is dinamically alocated.... When I pass it to other function, I need to determine size of this array.... Problem: sizeof operator does not work with dinamically alocated...
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
27
by: mike3 | last post by:
Hi. I can't believe I may have to use an array here. I've got this bignum package I was making in C++ for a fractal generator, and tried an approach that was suggested to me here a while...
9
by: Slain | last post by:
I have more of a conceptual question now. Let us say I do this:- char *str; --create an array of pointers str= "John"; I thought this would automatically put John at some memory space and...
4
by: Edward Jensen | last post by:
Hi, I have the following static arrays of different size in a class: in header: static double w2, x2; static double w3, x3; static double w4, x4; in GaussLegendre.cpp:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.