迭代器到std :: list中的最后一个元素 [英] Iterator to last element in std::list

查看:2552
本文介绍了迭代器到std :: list中的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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
}

iter到列表中的最后一个元素?

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

推荐答案

是的,你可以从一个回来。 (假设您知道该列表不为空。)

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;

这篇关于迭代器到std :: list中的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆