From: Manfred Doudar on

Hi all,

A query I hope someone might help with:

If I have an iterator "It" to an element of some container "C", is there
any way possible that I can get the address of "C" knowing "It"

The answer I know is No.

But if getting the address of the container is a requirement, what would
be the best to achieve that?

Right now I have a container K, holding N iterators _within_ N different
containers [M1 ... M_n], and would like to query the container address
to which an iterator in K belongs (one of the M_i's) - other than
holding a pair in K of std::pair(iterator_in_M_i,
container_address_of_M_i) [, instead of just iterators alone in K], what
would be a better alternative?


Thanks in advance,
--
Manfred

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: David Abrahams on
Manfred Doudar <manfred.doudar(a)rsise.anu.edu.au> writes:

> > Hi all,
> >
> > A query I hope someone might help with:
> >
> > If I have an iterator "It" to an element of some container "C", is
there
> > any way possible that I can get the address of "C" knowing "It"
> >
> > The answer I know is No.
> >
> > But if getting the address of the container is a requirement, what
would
> > be the best to achieve that?

Build special iterators that carry pointers to the containers within
them.
http://www.boost.org/libs/iterator/doc/index.html#iterator-facade-and-adaptor

HTH,

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Manfred Doudar wrote:
> Right now I have a container K, holding N iterators _within_ N different
> containers [M1 ... M_n], and would like to query the container address
> to which an iterator in K belongs (one of the M_i's) - other than
> holding a pair in K of std::pair(iterator_in_M_i,
> container_address_of_M_i) [, instead of just iterators alone in K], what
> would be a better alternative?

I'm not sure what you are trying to accomplish, maybe looking at the problem
from a greater distance would help. However, this seems to me like you have
N containers and for each container an additional iterator. If that is the
case, I'd consider bundling those together:

struct aggregate
{
// Danger with copying and assignment, disable or implement properly!
container c;
container::iterator it;
};


Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Victor Bazarov on
Manfred Doudar wrote:
> A query I hope someone might help with:
>
> If I have an iterator "It" to an element of some container "C", is
> there any way possible that I can get the address of "C" knowing "It"

&(*It)

> [...]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Martin Bonner on

Victor Bazarov wrote:
> Manfred Doudar wrote:
> > A query I hope someone might help with:
> >
> > If I have an iterator "It" to an element of some container "C", is
> > there any way possible that I can get the address of "C" knowing "It"
>
> &(*It)

That returns the address of the element, not the address of the
container!


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]