STL: vector<T> to T[]

N

nerdrakesh

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 ();
 
V

Victor Bazarov

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
 
B

Barry

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

Justin.SpahrSummers

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

Alf P. Steinbach

* (e-mail address removed):
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
 
J

Justin.SpahrSummers

* (e-mail address removed):
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.
 
R

red floyd

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

Barry

red said:
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
 
V

Victor Bazarov

Daniel said:
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
 
D

Daniel Kraft

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
 
J

Jeff F

Victor Bazarov said:
Daniel said:
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.
 
J

Jim Langston

Daniel Kraft said:
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.
 
A

Alf P. Steinbach

* Jim Langston:
Daniel Kraft said:
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?
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top