Iterators as associative containers' key

  • Thread starter =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
  • Start date
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Hello.

A non-modifying algorithm I implemented uses two associative containers
from STL: set and map. The elements on those containers are supposed to
refer to actual elements which lie on another, bigger container.

I had the definition of my auxiliary containers based on pointers, but
considered using iterators instead. I know iterators might become
invalid upon certain operations, but as I said, the algorithm is
non-modifying. This is a snippet of what I was trying:

set<int> s;
typedef set<int>::iterator ITER;

s.insert(1);

set<ITER> t;
t.insert(s.begin());

In spite of my attempts, I could not get the code to work when using
iterators as keys for the associative containers. This probably has
good, obvious reasons, which are not clear to me at the moment. Would
anybody be so kind to explain that which I am missing?

Thank you,
 
V

Victor Bazarov

Ney André de Mello Zunino said:
A non-modifying algorithm I implemented uses two associative containers
from STL: set and map. The elements on those containers are supposed to
refer to actual elements which lie on another, bigger container.

I had the definition of my auxiliary containers based on pointers, but
considered using iterators instead. I know iterators might become
invalid upon certain operations, but as I said, the algorithm is
non-modifying. This is a snippet of what I was trying:

set<int> s;
typedef set<int>::iterator ITER;

s.insert(1);

set<ITER> t;
t.insert(s.begin());

In spite of my attempts, I could not get the code to work when using
iterators as keys for the associative containers. This probably has
good, obvious reasons, which are not clear to me at the moment. Would
anybody be so kind to explain that which I am missing?

Set iterators do not define operator <, which is required if no
other comparison functor is provided for the Key type. You could
try defining your own comparison for type 'ITER' and pass it to
the set<ITER, myComparisonType>. The simplest I could imagine is

struct myComparisonType {
bool operator()(const ITER& i1, const ITER& i2) const {
return std::distance(i1, i2) < 0;
}
};

(or something like it). Try it, I used it and it seems to compile
but will it work I am not sure.

Victor
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Victor said:
struct myComparisonType {
bool operator()(const ITER& i1, const ITER& i2) const {
return std::distance(i1, i2) < 0;
}
};

(or something like it). Try it, I used it and it seems to compile
but will it work I am not sure.

You are right and your solution works great. Thank you very much for the
insight.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top