Re: iterator (adaptor) mysteries
On 3 Oct 2006 20:41:23 -0400
"albrecht.fritzsche" <albrecht.fritzsche@arcor.de> wrote:
In the quest for the simplest iterator adaptor possible I came
up with the idea of simply inheriting from the iterator I'd
like to adapt to, iw with code a la
#include <iostream>
#include <vector>
#include <iterator>
template<typename Iter>
class IteratorAdaptor : public Iter {
public:
IteratorAdaptor(const Iter& it) : it_(it) {}
typename Iter::value_type operator*() { return *it_; }
private:
Iter it_;
};
using namespace std;
int main() {
vector<int> array;
for (size_t i=0; i<10; ++i)
array.push_back(static_cast<int>(i));
IteratorAdaptor<vector<int>::const_iterator> it(array.begin());
IteratorAdaptor<vector<int>::const_iterator> end(array.end());
cout << *it << endl;
++it;
cout << *it << endl;
for (; it != end; ++it)
cout << *it << endl;
return 0;
}
_First_problem_
While this compiles on Vis C++ and on Dinkumware Exam (there even
with the EDG/C++ combination), I do get compile errors on Comeau
Online
"ComeauTest.c", line 6: error: not a class or struct name
class IteratorAdaptor : public Iter
^
detected during instantiation of class
"IteratorAdaptor<Iter> [with Iter=const int *]" at line 24
_Second_problem_
On Vis C++ I do get strange runtime errors in some internal validation
checks in the _Vector_const_iterator::operator++()
_SCL_SECURE_VALIDATE(this->_Mycont != NULL);
So, my question is - what it wrong with inheriting from an iterator
and then using this inherited class as an iterator?
Thanks for any help - I still have no idea where I have left
Standard C++.
Ali
Hi Ali,
Try this instead:
#include <iostream>
#include <vector>
#include <iterator>
template<typename Iter>
class IteratorAdaptor : public Iter
{
public:
IteratorAdaptor(Iter it)
: it_(it)
{
}
typename Iter::value_type operator*()
{
return *it_;
}
void operator++()
{
++this->it_;
}
void operator++(int)
{
this->it_++;
}
bool operator!=(Iter it)
{
return it_ != it;
}
bool operator!=(IteratorAdaptor it)
{
return it_ != it.it_;
}
private:
Iter it_;
};
int main()
{
std::vector<int> array;
for (int i = 0; i < 10; ++i)
{
array.push_back(i);
}
IteratorAdaptor<std::vector<int>::const_iterator> it(array.begin());
IteratorAdaptor<std::vector<int>::const_iterator> end(array.end());
std::cout << *it << std::endl;
++it;
std::cout << *it << std::endl;
for (; it != end; ++it)
{
std::cout << *it << std::endl;
}
return 0;
}
Note however, your IteratorAdaptor (, and the one presented above),
does not constitute a standards conforming iterator by any
measure, ... and you'll still need among other things to add the
necessary nested typedefs.
Writing your own iterator adaptors is error prone at the very least,
and should take the hard work out (to some degree), by using the Boost
Iterator Lib:
http://www.boost.org/libs/iterator/doc/index.html
HTH,
--
Manfred Doudar - Research Engineer
National ICT Australia - Canberra Research Lab | www.nicta.com.au
Research School of Information Sciences and Engineering (RSISE)
The Australian National University - Canberra, ACT 0200 AUSTRALIA
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]