#include <iostream> #include <set> #include <iterator> using namespace std; struct keker{ int a; int b; }; int main(int argc, char const *argv[]) { set<keker> rofl; keker k1,k2,k3,k4; k1.a = 1; k2.a = 2; k3.a = 3; k4.a = 4; rofl.insert(k1); rofl.insert(k2); rofl.insert(k3); rofl.insert(k4); cout<< rofl.begin()->a<<endl; cout<< rofl.end()->a<<endl; return 0; } 

After compilation displays: 1 1

Although it should output: 1 4

Please help me all day with it

  • In the example code, there is no comparison operator code (<) for keker - Chorkov

1 answer 1

Right here

  rofl.end()->a 

he can do what he wants at all: you cannot dereference the iterator returned by end() !

This is not the last element in the container, it is an iterator, indicating, roughly speaking, for the container!

All that it is suitable for is the comparison of the value of another iterator with it - the type, the passage through the container is completed, or the element is not found there.

But not for dereference!

  • Well, I did this: auto it = rofl.begin (); it ++; cout << rofl.begin () -> a; cout << it-> a; And still it outputs 1 1, although it should output 1 2. What am I doing wrong? - user264278
  • one
    You so confidently wrote that it displays something that I decided that you simply forgot to specify < for your keker type in the keker , but you have it. After all, your code simply does not compile without it - ideone.com/DG005R - set should know how to organize the elements. But if you add this operator, everything will be fine: ideone.com/6RRU99 Excuse me, of course, but do you have the exact code like this, as you showed? - Harry
  • And, I understood everything, apparently I did not fully read about set - user264278