jeudi 10 décembre 2020

Why is the end() element in Associative container print the same value as the last element?

In the code below, the value that the iterator points to is the same for both the last and the second last element.

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


int main() 
{
    set<int> s1 = {4,3,2,5,1};
    set<int>::iterator i;
    
    i = s1.end();
    cout << *i << endl; // 5
    
    i--;
    cout << *i << endl; // 5
    
    
    cout << *s1.end() << endl;  // 5
    cout << *(--s1.end()) << endl;  // 5
    
    return 0;
}

In my understanding the value pointed to by the end element should be null. Why is this so?

Aucun commentaire:

Enregistrer un commentaire