By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

getting an array from an STL vector

P: n/a
Hi, in Java I know that you can convert a vector into an array of
Object with toArray(). Is there a corresponding function in STL
that will immediately convert a vector of T into an array of T?

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Digital Puer wrote:
Hi, in Java I know that you can convert a vector into an array of
Object with toArray(). Is there a corresponding function in STL
that will immediately convert a vector of T into an array of T?


// v is the name of the vector, arr is the name of the array

std::copy(v.begin(), v.end(), arr);

Nov 22 '05 #2

P: n/a
On 2005-11-18 06:56, Neelesh Bodas wrote:
Digital Puer wrote:
Hi, in Java I know that you can convert a vector into an array of
Object with toArray(). Is there a corresponding function in STL
that will immediately convert a vector of T into an array of T?


// v is the name of the vector, arr is the name of the array

std::copy(v.begin(), v.end(), arr);


Or (a bit more ugly) as mentioned on another thread you can take the
address of the first object and use it as a array (since a vector will
store the elements contiguously).

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Nov 22 '05 #3

P: n/a

Digital Puer skrev:
Hi, in Java I know that you can convert a vector into an array of
Object with toArray(). Is there a corresponding function in STL
that will immediately convert a vector of T into an array of T?


A std::vector internally has an array of T. To get it use e.g. &v[0].
This probably is in the FAQ.

/Peter

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.