473,503 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL: vector<T> 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<intvt;
vt.push_back(1);
vt.push_back(2);

int [] arr = vt. xyz ();

Aug 2 '07 #1
10 1933
ne********@gmail.com wrote:
Is there a method in STL vector to get the elements as an array
instead of as a vector.

Something like

vector<intvt;
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
Aug 2 '07 #2
ne********@gmail.com wrote:
Hi -
Is there a method in STL vector to get the elements as an array
instead of as a vector.

Something like

vector<intvt;
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.
Aug 2 '07 #3
On Aug 1, 7:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
vector<intvt;
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.

Aug 2 '07 #4
On Aug 2, 12:44 am, "Alf P. Steinbach" <al...@start.nowrote:
* Justin.SpahrSumm...@gmail.com:
On Aug 1, 7:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>vector<intvt;
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.

Aug 2 '07 #5
Barry wrote:
ne********@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]
Aug 2 '07 #6
red floyd wrote:
Barry wrote:
>ne********@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
Aug 2 '07 #7
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!
Aug 2 '07 #8
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
Aug 2 '07 #9

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f8**********@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.
Aug 3 '07 #10
"Daniel Kraft" <d@domob.euwrote in message
news:f8**********@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.
Aug 5 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2250
by: Marc Schellens | last post by:
my dinkumware docu says, vector<...>::rbegin() returns an iterator which points just BEYOND the end of the controlled sequence. Is that true? so I cannot say: for( riter i=v.rbegin(); i !=...
2
6727
by: john smith | last post by:
Hi, when there is a vector<> of pointers to some objects, does calling resize cause vector to call delete on each object, or is there a memory leak problem? for example: struct base {//some...
10
7044
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
6
3767
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
4
2334
by: Anu | last post by:
Hi, We have a class that has its own overloaded operator new and whose prototype seems to correspond to the standard placement new :- class AppClass { public: operator new (size_t size,...
5
1939
by: Numeromancer | last post by:
From the C++-FAQ Lite: http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3 ---------------------------- 34.3] Is the storage for a std::vector<Tguaranteed to be contiguous? Yes. ...
8
3589
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
3
5633
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
1
2503
by: iammilind | last post by:
In one of my code, I was using vector<> for certain class. In one of my struct, I have 'const' member data. However, vector<>::clear() throws compile error with that:...
4
2467
by: iammilind | last post by:
I am not able to understand why the 2nd ~Test() is called with different 'this' but the same string value ?? Also why does it give error, if I want to declare Test::Str as a const string member ?? ...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.