Thread: [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-06 02:16:36
|
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? 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... 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. I guess that I could do this easier if I replaced the reciprical pointers with multiset iterators, but I want to make the Book and Author classes as basic as possible. I'm experimenting with the different STL containers. and I want to not have to adapt the classes for each one. Hoping this makes sense, Phil -- Fy...@qw... |
|
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 |
|
From: Ioannis V. <no...@ya...> - 2001-07-06 15:39:50
|
a small correction > e.g. vector<int *>integers(5); > integers[0]=new int(5); // it is like we assign then integers[0]=5; it is like we assign then *integers[0]=5; Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys |
|
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 |
|
From: Ioannis V. <no...@ya...> - 2001-07-07 13:14:27
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Phil Schulz
> Sent: Saturday, July 07, 2001 10:21 AM
> To: dev...@li...
> Subject: Re: [Dev-C++] STL, iterators and pointers?
>
> 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...
When you use pushback() and vector is full, new objects within the
vector are created.
> 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.
Code that works:
A)
#include <vector>
class A
{
};
int main()
{
vector<A> test(10);
A *p;
p=&test[0];
}
B)
#include <vector>
class A
{
};
int main()
{
vector<A> test(10);
vector<A>::iterator iter=test.begin();
A *p;
p=&(*iter);
}
Code that does not work (of course):
#include <vector>
class A
{
};
int main()
{
vector<A> test(10);
vector<A>::const_iterator iter=test.begin();
A *p;
p=&(*iter);
}
You probably were using const_iterator or doing something else wrong.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|