如何为指定的列表编写迭代器 [英] How can I write a iterator for list as specified
问题描述
我对list的迭代器有些困惑.我写了一段代码.
#include >< iostream >
#include < 列表 >
#include < 向量 >
#include < 算法 >
使用 命名空间 std;
int main()
{
vector< int> vector1;
for (未签名 int 索引= 0 ;索引< 10 ; ++ index)
{
vector1.push_back(index);
}
// 我可以在vector的迭代器上添加一个常量
find(vector1.begin(),vector1.begin()+ 5 , 2 ));
list< int> list1;
for (未签名 int 索引= 0 ;索引< 10 ; ++ index)
{
list1.push_back(index);
}
// ,但是当我尝试在列表中进行操作时,我发现它没有用.
find(list1.begin(),list1.begin()+ 5 , 2 );
}
如何为指定的列表编写迭代器?请帮助我.
简短答案:您不能说listl.begin() + 5
,因为列表迭代器不属于随机访问迭代器类别 [双向迭代器类别 [listl.begin() + 5即可获得第5个项目的迭代器.在向量中,迭代器实现很容易找出当前项目中五分之五的项目的迭代器,因为向量基本上是一个数组,因此步进迭代器只是一个加法.如果是链表,则要步进迭代器(效率低下),则必须执行N次链接查找.仍然可以允许列表迭代器使用恒定数量的项目来执行它,但stl容器不包含算法性能较慢的方法.他们的迭代器也是如此.要查看关于迭代器类别的含义,请阅读以下站点之一:
http://www.cplusplus.com/reference/std/iterator/RandomAccessIterator/ [ ^ ]
http://anaturb.net/C/iterators.htm [ list< int> ::: const iterartor it = listl.begin(); for ( int i = 0 ; i< ; 5; ++ i) ++ it;
如果< const> std :: list的迭代器包含随机访问运算符,则它们还应该使用for循环执行N的增量/减量,该循环非常无效并且看起来很奇怪.某些算法在处理列表时需要前进几步,但是如果您的代码包含巨大(可能是随机大小)的前进/后退步骤,那么您可能选择了错误的stl容器类型.
>
I am a little bit confused about iterator for list. I wrote a piece of code.
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int>vector1;
for(unsigned int index = 0; index < 10; ++index)
{
vector1.push_back(index);
}
// I can add a constant on the iterator of vector
find(vector1.begin(), vector1.begin() + 5, 2);
list<int>list1;
for(unsigned int index = 0; index < 10; ++index)
{
list1.push_back(index);
}
// but when I try to do it on list, I found it didn`t work.
find(list1.begin(), list1.begin() + 5, 2);
}
How can I write a iterator for list as specified? Please help me.
Short answer: You cant saylistl.begin() + 5
because the list iterator doesn''t fall into the random access iterator category[^]. The iterator of the stl list is in the bidirectional iterator category[^].
Long answer: There are iterator categories each of them giving an iterator some ''special abilities''. For example a random access iterator provides that you can access items by performing ''indexing'' with it like you did - you are basically getting the iterator for the 5th item by sayinglistl.begin() + 5
. In a vector its easy for an iterator implementation to find out the iterator of an item that is 5 past five the current item because the vector is basically an array so stepping the iterator is just an addition. In case of a linked list you have to perform N number of link lookups if you want to step your iterator, its inefficient. It would still be possible to allow for a list iterator to step it with a constant number of items but stl containers doesn''t contain methods whose performance is algorithmically slow. The same is true for their iterators. To check out what I mean on iterator categories read one of the following sites:
http://www.cplusplus.com/reference/std/iterator/RandomAccessIterator/[^]
http://anaturb.net/C/iterators.htm[^]
EDIT: If you still insist on writinglistl.begin() + 5
then you have to do that by incrementing the list iterator in a for loop with single steps by using one of the operations (++) available for bidirectional iterators:
list<int>::const iterartor it = listl.begin(); for (int i=0; i<5; ++i) ++it;
If the iterator of <const>std::list contained random access operators they should also do the increment/decrement by N with a for loop that is pretty ineffective and looks weird. Some algorithms require to step forward a few steps when processing a list but if your code contains huge (maybe randomly sized) forward/backward steps then you have probably chosen the wrong type of stl container.
这篇关于如何为指定的列表编写迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!