casting of almost identical iterators??

Hy
I cant solve the following problem:
I have 2 sets set<String, less1> and set<String, less2> which differ only in the ordering function. I want to store iterators from both sets in one vector, but the iterators have different types. Scince the ordering function is not importent for the iterator, it should be possible to cast one type into the other or to find a common base type, or to find a woraround,but i cant figure out how.

code:
// ordering functions

class less1: public std::binary_function<const class value_type& ,const class value_type& , bool> {"code1"};
class less2: public std::binary_function<const class value_type& ,const class value_type& , bool> {"code2"};

//sets

class index1_t : public set<value_type, less1>; index1_t index1;
class index2_t : public set<value_type, less2>; index2_t index2;

//function that does not work

vector<index1_t::iterator> find(value_type v)
{
vector<inder1_t::terator> res;
res.push_back(index1->lower_bound(v));
res.push_back(index2->lower_bound(v));
//this gives an error of cause scince ther is no type cast
return res;
};

I need to store the iterators, not only pointers on the elements( need ++ and -- to browse through lists).
I tried with unit, but apparently it is not possible to build a unit with 2 iterators.
I tried defining a constuctor:

index1_t::iterator(index2::iterator){???};

but apart from the fact, that i dont know what to write for ??? it doesnt work, because the constructor is apparently not used for the cast, and i get the same error.

Anybody any hints for me??

Thx a lot
Last edited on
Scince the ordering function is not importent for the iterator

Wrong. A set is most likely implemented as a balanced binary tree. The while not supported by the standard, some stl implementations allow set elements to be changed through an iterator. But then, upon traversal, the inetaror has to check the ordering, which is done by the less predicate.

I want to store iterators from both sets in one vector

Do you really require iterators, or would a pointer to the value suffice? In that case: stl containers require that the address of their elements can be taken (thats why std::vector<bool> is no container...), so you can use pointers instead. They are invalidated at the same time iterators into the container are invalidated.
Thx for the interesting comment.

True, i didn't think at the insertion via iterator.
Unfortunately i realy need the iterators, scince i also need to look at the neibour elements.
What i came up with is this:

index1* i=(index1*)&index2->lower_bound(v);
res.push_back(*i);

it compiles, but the question is if it runs/if browsing through elements works as it is meant to (at least as long as i dont insert anything).
What do you think? Is this a dangeroous aproach, or should i be fine?

By the way, do you know a source, were i can find more information about the internal structure of iterators / stl containers?

Thx


I don't use sets too often, and to tell you for sure I'd have to do a 1h+ research in the standard. However, that wouldn't be too helpful, since most implementations aren't 100% standard compliant (otherwise std::set::const_iterator would be equal to std::set::iterator, I think).
I'd try to wrap the iterators into a common object like:
1
2
3
4
5
6
7
8
struct iter_wrap
{
  iter_1 i1_;
  iter_2 i2_;
  iter_wrap(iter_1 i1) {i2_=my_set.end(); i1_ = i1;}
  iter_wrap(iter_2 i2) {i1_=my_set.end(); i2_ = i2;}
}
vector<iter_wrap> vi;

If that's feasable for you, it seems to be the best way (stl code and polymorphism don't mix too well... all classes are concrete, so no virtual destructors, so think twike before inheriting from them etc...)
By the way, do you know a source, were i can find more information about the internal structure of iterators / stl containers?

(Un?)fortunately, the standard doesn't say anything about how things have to work internally, just the interface is specified (where interface means more than just declarations. E.g., the concept "Assignable" means for type T: T has a copy constructor, a copy assignment operator, and copies are equivalent something like
1
2
3
4
5
6
T t;
T u(t);
T v;
v=u;
assert(t==u);
assert(t==v);

(not quite: Assignable doesn't imply Equality Comparable, but you get the spirit)
As a reference about the concepts, models and requirements in the STL, I use "Generic Programming and the STL" by Austern.
Last edited on
Thx a lot,
iter_wrap sounds like a good idea, and i'll try to implement it.

unfortunately i already had to notice several times, that stl and polimorphism are not mixing well. Is there an other library with similar structures, that is more object oriented?

Thx again for the really fast help.
I'm pretty sure there is, but I don't know about it ;-)
The stl is contained in the standard, so it should be preferred if somehow usable. If you tell us what exactly you want to achieve, perhaps we can help you to find a better solution than the wrapper class (most likely there is one), as these issues are design-related.
Topic archived. No new replies allowed.