473,657 Members | 2,523 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 1945
ne********@gmai l.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********@gmai l.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.b egin(), vec.end(), arr);

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

std::vector<int >::const_iterat or 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...@com Acast.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.no wrote:
* Justin.SpahrSum m...@gmail.com:
On Aug 1, 7:30 pm, "Victor Bazarov" <v.Abaza...@com Acast.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********@gmai l.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(actua lly pointer) to the vector content,
you can do this way:

std::vector<int >::const_iterat or 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********@gmai l.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(actua lly pointer) to the vector content,
you can do this way:

std::vector<in t>::const_itera tor 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.********@com Acast.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

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

Similar topics

0
2260
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 != v.rend(); i++) { something = (*i); }
2
6743
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 vars; ~base();}; vector<base*> vb; vb.push_back(new base); vb.push_back(new base);
10
7066
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 clear(), in DOT.NET the capacity() is reduced to 0.
6
3772
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; strings.push_back("abc"); strings.push_back("xyz"); strings.push_back("lmnop");
4
2338
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, void *ctx)
5
1952
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. This means you the following technique is safe: #include <vector>
8
3608
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 class simple_vector<that is a hand-rolled substitute for array<>. With the latter I have code that tracks all allocations and destructions, so I can account for all the memory. The question is about std::vector<-- how can I track memory usage
3
5636
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 k = i+1;
1
2521
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: =========================================== struct Test { const string Str; Test(const string str) : Str(str) {} }; struct TestWrapper
4
2480
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 ?? ======================================================= struct Test{ string Str; // Gives error if, you declare const string ! Test(const string s) : Str(s) { cout<<Str<<" Test() "<<this<<endl; } ~Test() {...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2735
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 we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.