RE: [Dev-C++] STL, iterators and pointers?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2001-07-06 15:32:21
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Phil Schulz > Sent: Friday, July 06, 2001 4:17 AM > To: dev...@li... > Subject: [Dev-C++] STL, iterators and pointers? > > > > 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? 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]; Now if you create a vector of pointers e.g. vector<string *> pointers(10); pointers[0]=&s; pointer[0] is pointing to our string s; > > 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? e.g. vector<int *>integers(5); integers[0]=new int(5); // it is like we assign then integers[0]=5; vector<int *>::iterator iter=integers.begin(); int *p; p=*iter // and NOT &(*iter) since the container contains pointers and our iterator behaves like int ** > > Why I'm asking: > I have two classes, Book and Author that each contain a > pointer to the other. > > I have a multiset of Books, and a multiset of Authors. When I > add a new Book, I need to be able to link it to an Author, > and vice versa. The > Book* I get when I make a new object doesn't correspond to > where the iterator is pointing. Probably you made some mistakes on the assignment, check what i wrote above. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys |