mardi 24 juillet 2018

Traversing stl list till end() -1 in C++

I need to traverse stl list in c++ till end() - 1, what is the efficient way to do this?

#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> l{1,2,3,4,5};

    for(auto itr = l.begin(); itr != l.end() - 1; ++itr)
    {   
        cout << *itr << '\n';
    }   
}

Aucun commentaire:

Enregistrer un commentaire