STL: vector to T[]



 DEVELOP > c-Plus-Plus > STL: vector to T[]

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 01 Aug 2007 06:59:23 PM
Object: STL: vector to T[]
Hi -
Is there a method in STL vector to get the elements as an array
instead of as a vector.
Something like
vector<int> vt;
vt.push_back(1);
vt.push_back(2);
int [] arr = vt. xyz ();
.

User: "Barry"

Title: Re: STL: vector to T[] 02 Aug 2007 12:17:39 AM
wrote:

Hi -
Is there a method in STL vector to get the elements as an array
instead of as a vector.

Something like

vector<int> vt;
vt.push_back(1);
vt.push_back(2);

int [] arr = vt. xyz ();

I think your program habit is heavily affected by Java,
It's not that often you need to convert a vector in C++ into an array,
because of the vector implementation in C++, and the cooperation of
`vector', `iterator' and `algorithm'.
If you really wanna get a copy of the vector content into an array,
you can it like this:
int* arr = new int[vec.size()];
std::copy(vec.begin(), vec.end(), arr);
Or if you just have a reference(actually pointer) to the vector content,
you can do this way:
std::vector<int>::const_iterator it = vec.begin();
but I think the latter one is unusefull.
.
User: "red floyd"

Title: Re: STL: vector to T[] 02 Aug 2007 01:40:48 AM
Barry wrote:

nerdrakesh@gmail.com wrote:

Hi -
Is there a method in STL vector to get the elements as an array
instead of as a vector.

Or if you just have a reference(actually pointer) to the vector content,
you can do this way:

std::vector<int>::const_iterator it = vec.begin();

No, you can't do it that way. Repeat after me. An iterator is not a
pointer. It can be implemented as one, and I really can't see why a
vector iterator wouldn't be implemented as such (modulo checked iterators).
If you want a pointer to the data (which is guaranteed to be
contiguous), use &vec[0]
.
User: "Barry"

Title: Re: STL: vector to T[] 02 Aug 2007 02:26:57 AM
red floyd wrote:

Barry wrote:

nerdrakesh@gmail.com wrote:

Hi -
Is there a method in STL vector to get the elements as an array
instead of as a vector.


Or if you just have a reference(actually pointer) to the vector content,
you can do this way:

std::vector<int>::const_iterator it = vec.begin();


No, you can't do it that way. Repeat after me. An iterator is not a
pointer. It can be implemented as one, and I really can't see why a
vector iterator wouldn't be implemented as such (modulo checked iterators).

If you want a pointer to the data (which is guaranteed to be
contiguous), use &vec[0]

Got your point,
I am influenced by much by the sgi sTL,
which implement vector in this way
typedef value_type* pointer;
typedef value_type* iterator;
typedef value_type const* const_poiter;
typedef value_type const* const_iterator;
anyway, assume vec.begin() as Tp* is bad idea
thx
.



User: "Victor Bazarov"

Title: Re: vector to T[] 01 Aug 2007 07:30:13 PM
wrote:

Is there a method in STL vector to get the elements as an array
instead of as a vector.

Something like

vector<int> vt;
vt.push_back(1);
vt.push_back(2);

int [] arr = vt. xyz ();

First of all, you cannot declare an array like that. Brackets
follow the name and they need to contain the size.
Second, arrays cannot be initialised like that. You need to use
pointers:
int *arr = &vt[0];
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: ""

Title: Re: vector to T[] 02 Aug 2007 12:33:22 AM
On Aug 1, 7:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

vector<int> vt;
vt.push_back(1);
vt.push_back(2);


int [] arr = vt. xyz ();


First of all, you cannot declare an array like that. Brackets
follow the name and they need to contain the size.

As far as I'm aware (please correct me if I'm wrong, as I don't have a
copy of the C++ standard), std::vector does have a default constructor
that automatically gets called in that declaration, and will construct
a vector with a size of zero. Since vectors are dynamic, the following
invocations of push_back() automatically resize the internal storage
to the necessary size.
.
User: "Alf P. Steinbach"

Title: Re: vector to T[] 02 Aug 2007 12:44:40 AM
* Justin.SpahrSummers@gmail.com:

On Aug 1, 7:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

vector<int> vt;
vt.push_back(1);
vt.push_back(2);
int [] arr = vt. xyz ();

First of all, you cannot declare an array like that. Brackets
follow the name and they need to contain the size.


As far as I'm aware (please correct me if I'm wrong, as I don't have a
copy of the C++ standard), std::vector does have a default constructor
that automatically gets called in that declaration, and will construct
a vector with a size of zero.

Yes.
But Victor was referring to the declaration
int [] arr = ...
which does not follow C++ syntax.
Anyway, to get at the internal array in a vector v, e.g. for the
purposes of passing to some C function, simply do &v[0].

Since vectors are dynamic, the following
invocations of push_back() automatically resize the internal storage
to the necessary size.

Yes.
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "Daniel Kraft"

Title: Re: vector to T[] 02 Aug 2007 12:40:56 PM

Anyway, to get at the internal array in a vector v, e.g. for the
purposes of passing to some C function, simply do &v[0].

This works of course for the usual concept of a vector -- but is this
guaranteed to work? To me it seems like maybe safe but still dirty hack...
Cheers,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
.
User: "Jim Langston"

Title: Re: vector to T[] 05 Aug 2007 03:28:15 AM
"Daniel Kraft" <d@domob.eu> wrote in message
news:f8stu1$7m2$1@newsreader2.utanet.at...

Anyway, to get at the internal array in a vector v, e.g. for the purposes
of passing to some C function, simply do &v[0].


This works of course for the usual concept of a vector -- but is this
guaranteed to work? To me it seems like maybe safe but still dirty
hack...

As V says, it is required to work, and is the way it's normally done. I
just wish that std::string had this same requirement, would make things a
lot easier.
.
User: "Alf P. Steinbach"

Title: Re: vector to T[] 05 Aug 2007 03:54:06 AM
* Jim Langston:

"Daniel Kraft" <d@domob.eu> wrote in message
news:f8stu1$7m2$1@newsreader2.utanet.at...

Anyway, to get at the internal array in a vector v, e.g. for the purposes
of passing to some C function, simply do &v[0].

This works of course for the usual concept of a vector -- but is this
guaranteed to work? To me it seems like maybe safe but still dirty
hack...


As V says, it is required to work, and is the way it's normally done. I
just wish that std::string had this same requirement, would make things a
lot easier.

It has, in two senses. (1) The in-practice, no current standard library
implementation is known to not support that. (2) The in-future, the
library working group adopted contiguous storage for strings about a
year (or is it now two years?) ago, Lillehammer meeting, same as for
vector, and that will be in C++0x.
So, things /are/ actually a lot easier. ;-)
However, the lack of a standard read-only string carrier class (with
specificiable destruction function) means that less than half of the
solution is in place in C++. That's not very sensible. If I weren't so
darned sure that nothing would eventually come of it, or that if
something came of it I'd not have the means to follow it through, I'd
make a proposal -- but hey, the world is full of people, a great
number of them are C++ programmers, some of those are even competent,
and there just must be at least one of the competent who can do this?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.


User: "Victor Bazarov"

Title: Re: vector to T[] 02 Aug 2007 10:50:49 AM
Daniel Kraft wrote:

Anyway, to get at the internal array in a vector v, e.g. for the
purposes of passing to some C function, simply do &v[0].


This works of course for the usual concept of a vector -- but is this
guaranteed to work? To me it seems like maybe safe but still dirty
hack...

It is guaranteed to work with all conforming implementations of the
standard library. The Standard requires 'std::vector' to have its
elements in contiguous storage.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "Jeff F"

Title: Re: vector to T[] 02 Aug 2007 07:17:56 PM
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:f8sugn$10b$1@news.datemas.de...

Daniel Kraft wrote:

Anyway, to get at the internal array in a vector v, e.g. for the
purposes of passing to some C function, simply do &v[0].


This works of course for the usual concept of a vector -- but is this
guaranteed to work? To me it seems like maybe safe but still dirty
hack...


It is guaranteed to work with all conforming implementations of the
standard library. The Standard requires 'std::vector' to have its
elements in contiguous storage.

Assuming of course that v.empty() != true.
Jeff F.
.



User: ""

Title: Re: vector to T[] 02 Aug 2007 01:36:32 AM
On Aug 2, 12:44 am, "Alf P. Steinbach" <al...@start.no> wrote:

* Justin.SpahrSumm...@gmail.com:

On Aug 1, 7:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

vector<int> vt;
vt.push_back(1);
vt.push_back(2);
int [] arr = vt. xyz ();

First of all, you cannot declare an array like that. Brackets
follow the name and they need to contain the size.


As far as I'm aware (please correct me if I'm wrong, as I don't have a
copy of the C++ standard), std::vector does have a default constructor
that automatically gets called in that declaration, and will construct
a vector with a size of zero.


Yes.

But Victor was referring to the declaration

int [] arr = ...

which does not follow C++ syntax.

Right! Very true. I went braindead for a second there and looked at
the wrong code. My apologies, Victor.
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER