Re: [Dev-C++] STL, iterators and pointers?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Phil S. <fy...@qw...> - 2001-07-07 08:19:44
|
I wrote: > > Some questions about the STL containers and iterators. > > > > When you insert an object into a multiset (or list, etc.) > > does it create a copy of the original Object, or does it > > connect to the original? > Ioannis Vranos wrote: > When you say "multiset" i consider that you mean standard library > container. When you create an object of a container type containing a > standard object type then the number specified of objects are created. > > E.g.. > > vector<string> l(10); creates an array (vector) of 10 string objects > with the name l; These 10 string objects are created with the creation > of the vector. > > so if we have string s="Test"; > > l[0]=s; > > s is COPIED in l[0]; okay, that's pretty clear. I had been looking at examples that start with defining an empty container and then adding to it using pushback() or insert(), so it wasn't clear to me... I forgot that I probably have to define copy constructors for my classes. Haven't tested this yet, but this might be the problem... > > > > and I'm not sure how to ask this next one: > > If you have an iterator for the above multiset, can you get a generic > > Object* out of it that points to the Object? I tried taking > > the addressof the dereferenced iterator ( &(*iter) ) but that > > didn't seem to work out quite right... > > > > iterators act like pointers but they are not pointers. the &(*iter) of > yours should work. Perhaps the container contained pointers and not > objects? nope, definitely defined as objects. My problem is when I try to assign &(*iter) to a pointer, I get compile errors "initialization to `Book *' from `const Book *' discards qualifiers" I'll make the new pointer a const, but that'll require another function to be declared a CONST and on and on , to a point of uselessness. > Ioannis > > * Ioannis Vranos > * Programming pages: http://www.noicys.f2s.com > * Alternative URL: http://run.to/noicys > Thanks for your input. Phil |