Q:

Error on `it-1` with set iterator

In following code I can not recognize difference between --it, and it-1.
Also is words.end() value is out of set range?
#include <iostream>
#include<set>

using namespace std;

int main()
{
    set<string> words;
    words.insert("test");
    words.insert("crack");
    words.insert("zluffy");
    set<string>::iterator it=words.end();
    cout<<*(--it)<<endl;//just works fine 
    cout<<*(it-1)<<endl;//Error 
    return 0;
}

A:

That's because std::set::iterator is a constant BidrectionalIterator. That means it supports ++it and --it. However, it is not a RandomAccessIterator (like std::vector::iterator) - which is what it would need to be to support it + n and it - n.
While --it and it -= 1 conceputally do the same thing, the latter requires more functionality than std::set provides. That's why it's not supported. You could always use something like: std::prev(it, 1), which would work on any iterator that satisfies the BidirectionalIterator concept.

