STL 不提供通过索引返回迭代器的函数有什么原因吗?

发布于 2024-08-18 02:27:27 字数 280 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

§对你不离不弃 2024-08-25 02:27:27

您可以使用 标头中的 advance()


list::iterator iter = advance(someFooList.begin(), n);

list<foo>::iterator iter = someFooList.begin();

std::advance( iter, n);

如果迭代器支持随机访问(如vector),那么它会非常高效地工作,如果它只支持增加(或减少)迭代器,如list,它会起作用,但也只能达到最好的程度。

You can use advance() from the <iterator> header:


list<foo>::iterator iter = advance(someFooList.begin(), n);

list<foo>::iterator iter = someFooList.begin();

std::advance( iter, n);

If the iterator supports random access (like vector) it'll work quite efficiently, if it only supports increasing (or decreasing) the iterator, like list, it'll work but only as well as can be.

得不到的就毁灭 2024-08-25 02:27:27

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.

好久不见√ 2024-08-25 02:27:27

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..

扎心 2024-08-25 02:27:27

一般来说,任何可能昂贵的东西都会变得有点笨拙,所以你不会偶然做的。使用您的示例,对于提供随机访问迭代器的容器,它只是 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 an std::list (which provides a forward iterator) you'd need to use list.begin() followed by advance().

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文