问题描述
大家好,
List及其迭代器按以下方式工作:
list< intmylist;
list< int> :: iterator itr;
itr = mylist.begin();
cout<< (* itr);
但是我想要这样的东西:
list< intmylist;
MyIterator itr (mylist);
cout<< (* itr);
所以我按照以下方式写了MyIterator:
template< class Tclass MyIterator:public list< T> :: iterator
{
public:
MyIterator(){};
~MyIterator(){};
MyIterator(列表< T>& mylist){
/ *我应该写在这里* /
};
};
int main()
{
list< intmylist;
mylist.push_back(1);
mylist.push_back(2);
/ *我想支持以下构造* /
MyIterator iter(mylist);
cout<< * mylist;
返回0 ;
}
有人可以指导我在一个参数中编写的内容
构造函数吗?或者你们有不同的解决方案吗?
我从list< T> :: iterator派生MyIterator的原因是我想要
来支持所有的重载运算符列表< T> :: iterator支持。
谢谢
Jayesh Shah
有什么区别? MyIterator需要是一些模板,因为
否则你无法区分不同的元素类型。所以它可能是
可能是MyIterator< int>。我看不出有什么区别
list< int> :: iterator。
Markus
为什么?对我来说看起来像BadIdea(tm)。
注意:不保证列表< T> :: iterator是可继承的。但是,
这在实践中不会成为问题,因为据我所知,所有广泛使用的实现都将list<> :: iterator实现为一个没有的类
敲定技巧。
你需要构建基数:
MyIterator(list< T& mylist)
:list< T> :: iterator(mylist.begin())
{}
(未经测试)
你打算如何在循环中使用这些迭代器:
MyIterator itr( mylist);
while(itr!= ???){
...
++ itr;
}
是:使用mylist.begin()和mylist.end()。
[snip]
Best
Kai-Uwe Bux
我会承认我没有测试过这个,但我很确定
STL-iterators有副本 - 构造函数所以这样的事情应该是b $ b工作:
#include< list>
#include< iostream>
typedef MyList std :: list< int>
typedef MyIterator std :: list< int> :: iterator
MyList l;
MyIterator itr(l.begin());
std :: cout<< * itr;
如果你真的想按照自己的方式去做,我认为你需要像这样初始化派生类的
基数(再次,未经测试):
#include< list>
#include< iostream>
template< class Tclass MyIterator:public list< T> :: iterator
{
MyIterator(std :: list< T>& l)
:std :: list< T> :: iterator(l.begin())
{};
};
int main()
{
std :: list< intl;
l.push_back(2);
MyIterator< intiter(l);
std :: cout<< * iter;
返回0;
}
注意模板参数,就像Markus指出的那样,创建
一个MyIterator级的实例。
-
Erik Wikstr?m
Hi All,
List and its iterator work as following way :
list<intmylist;
list<int>::iterator itr;
itr = mylist.begin();
cout << (*itr);
But I want something like this:
list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
So I wrote MyIterator following way :
template <class Tclass MyIterator : public list<T>::iterator
{
public:
MyIterator() { };
~MyIterator() { };
MyIterator(list<T>& mylist) {
/* What I should write here */
};
};
int main()
{
list<intmylist;
mylist.push_back(1);
mylist.push_back(2);
/* I want to support following construct */
MyIterator iter(mylist);
cout <<*mylist;
return 0;
}
Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?
The reason I derived MyIterator from list<T>::iterator is that I want
to support all the overloaded operator that list<T>::iterator supports.
Thanks
Jayesh Shah
What is the difference? MyIterator will need to be some template, as
otherwise you cannot distinguish different element types. So it would
probably be MyIterator<int>. I can''t see any difference to
list<int>::iterator.
Markus
Why? Looks like a BadIdea(tm) to me.
Note: there is no guarantee that list<T>::iterator is inheritable. However,
this will not be a problem in practice since, as far as I know, all widely
used implementations implement list<>::iterator as a class without
finalizing trickery.
You need to construct the base:
MyIterator ( list<T& mylist )
: list<T>::iterator( mylist.begin() )
{}
(untested)
And how do you intend to use these iterators in a loop:
MyIterator itr ( mylist );
while ( itr != ??? ) {
...
++itr;
}
Yes: use mylist.begin() and mylist.end().
[snip]
Best
Kai-Uwe Bux
I''ll admit that I have not tested this but I''m quite sure that
STL-iterators have a copy-constructor so something like this ought to
work:
#include <list>
#include <iostream>
typedef MyList std::list<int>
typedef MyIterator std::list<int>::iterator
MyList l;
MyIterator itr(l.begin());
std::cout << *itr;
If you realy want to do it your way I think you need to initialize the
base of your derived class like this (again, untested):
#include <list>
#include <iostream>
template <class Tclass MyIterator : public list<T>::iterator
{
MyIterator (std::list<T>& l)
: std::list<T>::iterator(l.begin())
{ };
};
int main()
{
std::list<intl;
l.push_back(2);
MyIterator<intiter(l);
std::cout << *iter;
return 0;
}
Notice the template parameter, like Markus pointed out, when creating
an instance of the MyIterator-class.
--
Erik Wikstr?m
这篇关于STL list :: iterator问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!