[英]It fails to define a const_iterator from it's counterpart iterator
I am extending stl container with a self defined container so as to provide more flexible control on the operations of the elements 我正在用自定义容器扩展stl容器,以便对元素的操作提供更灵活的控制
class MyContainer;
template <typename T> class myiterator :public iterator<bidirectional_iterator_tag, T>
{
friend class MyContainer;
private:
T *pointer;
myiterator(T *pt):pointer(pt) {}
public:
T& operator*() {return (*pointer);}
const myiterator<T>& operator++()
{
pointer->current_iterator++;
return *this;
}
bool isEnd(void) const
{
return pointer->current_iterator == pointer->data.end();
}
};
class MyContainer
{
friend class myiterator<MyContainer>;
public:
typedef myiterator<MyContainer> iterator;
typedef myiterator<MyContainer const> const_iterator;
private:
map<int, int> data;
map<int, int>::const_iterator current_iterator;
public:
MyContainer() {current_iterator = data.begin(); }
void addDataPair(int key, int value) {data[key] = value;}
int first() const {return (*current_iterator).first;}
int second() const {return (*current_iterator).second;}
iterator begin()
{
current_iterator = data.begin();
return iterator(this);
}
const_iterator begin() const
{
return const_iterator(this);
}
};
This code run ok, if I use iterator as follow 如果我按以下方式使用迭代器,则此代码可以正常运行
MyContainer h;
h.addDataPair(1, 1);
h.addDataPair(2, 2);
h.addDataPair(3, 3);
for (MyContainer::iterator it=h.begin(); !it.isEnd(); ++it)
{
cout << (*it).first() << " " << (*it).second() << endl;
}
But it won't compile if I change iterator to const_iterator. 但是,如果我将迭代器更改为const_iterator,它将无法编译。 I read some article, which mention that to define constant iterator, we simply replace the value_type from X to X const, that's how I did in my code.
我读了一些文章,其中提到要定义常量迭代器,我们只需将value_type从X替换为X const,这就是我在代码中所做的事情。 But I soon find that it might won't work in my case because the reference returned by an iterator is the container itself in my case.
但是我很快发现在我的情况下它可能行不通,因为在我的情况下,迭代器返回的引用是容器本身。 I don't know then how to make the const_iterator work without duplicate the coding.
我不知道该如何在不重复编码的情况下使const_iterator工作。
In addition, my iterator is derived from std::iterator but I found that I cannot override the constructor for my iterator. 另外,我的迭代器是从std :: iterator派生的,但是我发现无法覆盖我的迭代器的构造函数。 Is that any way that I can pass more than one parameters into my iterator besides the T *pt ?
除了T * pt之外,我还能以其他方式将多个参数传递给迭代器吗? Thanks.
谢谢。
First problem: 第一个问题:
If you change this: 如果您更改此设置:
for (MyContainer::iterator it=h.begin(); !it.isEnd(); ++it)
to 至
for (MyContainer::const_iterator it=h.begin(); !it.isEnd(); ++it)
then you get a non-const iterator
from begin()
and end()
and try to initialize a const_iterator
from it, but that's a different type and your my_iterator
template doesn't have a constructor that allows construction from a different type. 然后从
begin()
和end()
获得一个非常量iterator
,并尝试从中初始化const_iterator
,但这是一种不同的类型,而my_iterator
模板没有允许使用其他类型进行构造的构造函数。
You can fix that by adding: 您可以通过添加以下内容解决此问题:
template<typename> friend class myiterator;
template<typename T2>
myiterator(myiterator<T2> const& i) : pointer(i.pointer) { }
You should also make operator*
const (it doesn't alter the iterator to dereference it.) 您还应该将
operator*
设为const(它不会更改迭代器来取消对其的引用。)
But there's still a bigger problem, a const_iterator
points to a const MyContainer
, but const_iterator::operator++
needs to alter that object, which it can't because it's const. 但是还有一个更大的问题,
const_iterator
指向const MyContainer
,但是const_iterator::operator++
需要更改该对象,但不能更改,因为它是const。 So you can't increment your const_iterator
ie can't use it to iterate! 因此,您无法递增
const_iterator
即无法使用它进行迭代! You might want to rethink that design. 您可能需要重新考虑该设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.