STL 不提供通过索引返回迭代器的函数有什么原因吗?
STL 不提供通过索引将迭代器返回到容器中的函数是有原因的吗?
例如,假设我想将一个元素插入到 std::list 中,但在第 n 个位置。看来我必须通过诸如 begin()
之类的方式检索迭代器,并将 n
添加到该迭代器。我想如果我可以在第 n 个位置使用 std::list::get_nth_iterator(n)
之类的东西获取迭代器,会更容易。
我怀疑我误解了STL的原理。谁能帮忙解释一下吗?
谢谢 蜜蜂乐队
Is there a reason that the STL does not provide functions to return an iterator into a container via an index?
For example, let's say I wanted to insert an element into a std::list
but at the nth position. It appears that I have to retrieve an iterator via something like begin()
and add n
to that iterator. I'm thinking it would be easier if I could just get an iterator at the nth position with something like, std::list::get_nth_iterator(n)
.
I suspect I have misunderstood the principles of the STL. Can anyone help explain?
Thanks
BeeBand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
标头中的advance()
:list::iterator iter = advance(someFooList.begin(), n);
如果迭代器支持随机访问(如
vector
),那么它会非常高效地工作,如果它只支持增加(或减少)迭代器,如list
,它会起作用,但也只能达到最好的程度。You can use
advance()
from the<iterator>
header:list<foo>::iterator iter = advance(someFooList.begin(), n);
If the iterator supports random access (like
vector
) it'll work quite efficiently, if it only supports increasing (or decreasing) the iterator, likelist
, it'll work but only as well as can be.std::list
是一个链表。所以它不支持随机访问。要到达列表中的第 n 个位置,您必须从头开始并遍历所有节点,直到到达 n。这是相当昂贵的(O(n)),因此有一个不建议这种费用的方法是不好的。get_nth_iterator(n)
意味着获取指向第 n 个节点的迭代器是很便宜的。std::vector
当然可以使用[]
运算符直接支持这一点,因为数据结构支持随机访问,因此这对于它来说非常便宜。std::list
is a linked list. So it does not support random access. To get to the nth position in the list, you have to start at the beginning and move through all the nodes until you arrive at n. This is pretty expensive (O(n)), and thus it's bad to have a method that does not suggest this expense.get_nth_iterator(n)
implies getting the iterator that points to the nth node is cheap.std::vector
of course supports this directly with the[]
operator, because the datastructure supports random access and so this is very inexpensive for it.std::list 不是随机访问容器,因此没有理由访问第 n 个元素。如果您需要这个,请考虑使用 std::vector 代替..
std::list isn't random-access container, so there is no reason for accessing n-th element. if you need this, consider using std::vector instead..
一般来说,任何可能昂贵的东西都会变得有点笨拙,所以你不会偶然做的。使用您的示例,对于提供随机访问迭代器的容器,它只是
container.begin()+n
,但是对于std::list
(它提供了一个前向迭代器)迭代器),您需要使用list.begin()
,然后使用advance()
。如果您想获得第 Nth 迭代器,很可能您一开始就不应该使用
std::list
。话又说回来,如果你从“机会”开始这句话,那么它几乎仍然是正确的......Generally, anything that might be costly is made a bit clumsy, so you won't do it by accident. Using your example, with a container that provides random access iterators it would be simply
container.begin()+n
, but for anstd::list
(which provides a forward iterator) you'd need to uselist.begin()
followed byadvance()
.If you want to get the Nth iterator, chances are that you just shouldn't be using
std::list
in the first place. Then again, if you started that sentence at "chances", it would remain nearly as true...