指向 std::list 中最后一个元素的迭代器

发布于 2024-08-29 13:35:05 字数 313 浏览 5 评论 0原文

#include <list>
using std::list;

int main()
{
    list <int> n;
    n.push_back(1);
    n.push_back(2);
    n.push_back(3);

    list <int>::iterator iter = n.begin();
    std::advance(iter, n.size() - 1); //iter is set to last element
}

还有其他方法可以迭代到列表中的最后一个元素吗?

#include <list>
using std::list;

int main()
{
    list <int> n;
    n.push_back(1);
    n.push_back(2);
    n.push_back(3);

    list <int>::iterator iter = n.begin();
    std::advance(iter, n.size() - 1); //iter is set to last element
}

is there any other way to have an iter to the last element in list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

铃予 2024-09-05 13:35:05

是的,你可以从最后倒回去。 (假设您知道该列表不为空。)

std::list<int>::iterator i = n.end();
--i;

Yes, you can go one back from the end. (Assuming that you know that the list isn't empty.)

std::list<int>::iterator i = n.end();
--i;
狼亦尘 2024-09-05 13:35:05

以下任一操作都会将 std::list::iterator 返回到 list 中的最后一项:

std::list<int>::iterator iter = n.end();
--iter;

std::list<int>::iterator iter = n.end();
std::advance(iter, -1);

// C++11
std::list<int>::iterator iter = std::next(n.end(), -1);

// C++11
std::list<int>::iterator iter = std::prev(n.end());

以下代码将 std::list::reverse_iterator 返回到 list 中的最后一项:

std::list<int>::reverse_iterator iter = std::list::rbegin();

Either of the following will return a std::list<int>::iterator to the last item in the list:

std::list<int>::iterator iter = n.end();
--iter;

std::list<int>::iterator iter = n.end();
std::advance(iter, -1);

// C++11
std::list<int>::iterator iter = std::next(n.end(), -1);

// C++11
std::list<int>::iterator iter = std::prev(n.end());

The following will return a std::list<int>::reverse_iterator to the last item in the list:

std::list<int>::reverse_iterator iter = std::list::rbegin();
不再让梦枯萎 2024-09-05 13:35:05

使用反向迭代器:

iter = (++n.rbegin()).base()

附带说明:此方法或 Charles Bailey 方法具有恒定的复杂性,而 std::advance(iter, n.size() - 1); 具有列表的线性复杂性 [因为它具有双向迭代器]。

With reverse iterators:

iter = (++n.rbegin()).base()

As a side note: this or Charles Bailey method have constant complexity while std::advance(iter, n.size() - 1); has linear complexity with list [since it has bidirectional iterators].

如日中天 2024-09-05 13:35:05

使用 end() 并向后退一步。

list <int>::iterator iter = n.end();
cout << *(--iter);

Take the end() and go one backwards.

list <int>::iterator iter = n.end();
cout << *(--iter);
随波逐流 2024-09-05 13:35:05
std::list<int>::iterator iter = --n.end();
cout << *iter;
std::list<int>::iterator iter = --n.end();
cout << *iter;
独孤求败 2024-09-05 13:35:05

您可以编写自己的函数来从给定的迭代器中获取上一个(和下一个)迭代器(当我需要使用 std::list< 进行“后视”和“前视”时,我使用了该迭代器) /code>):

template <class Iter>
Iter previous(Iter it)
{
    return --it;
}

然后:

std::list<X>::iterator last = previous(li.end());

顺便说一句,这也可能在 boost 库中可用 (

You could write your own functions to obtain a previous (and next) iterator from the given one (which I have used when I've needed "look-behind" and "look-ahead" with a std::list):

template <class Iter>
Iter previous(Iter it)
{
    return --it;
}

And then:

std::list<X>::iterator last = previous(li.end());

BTW, this might also be available in the boost library (next and prior).

飞烟轻若梦 2024-09-05 13:35:05
list<int>n;
list<int>::reverse_iterator it;
int j;

for(j=1,it=n.rbegin();j<2;j++,it++)
cout<<*it;
list<int>n;
list<int>::reverse_iterator it;
int j;

for(j=1,it=n.rbegin();j<2;j++,it++)
cout<<*it;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文