Q:

Erasing element from a Set using value or iteraor

I was trying to implement the problem of returning all the subsets of a set using SET. My code is as follows:
set< set<int> > getallsets(set<int> iset){
    std::set< set<int> > sets;
    std::set<int> nullset;
    sets.insert(nullset);
    if(iset.size() <= 0)
        return sets;
    if(iset.size() ==1){
        sets.insert(iset);
        return sets;
    }

    std::set<int>::iterator it = iset.begin();
    set<int> niset = iset;
    ////////////////////////////
    niset.erase(*it);   // niset.erase(it); // Issue
    ///////////////////////////
    sets  = getallsets(niset);
    std::set< set<int> >::iterator i;
    std::set<int>::iterator j;
    for(i = sets.begin();i !=sets.end();i++){
        std::set<int> temp = *i;
        temp.insert(*it);
        sets.insert(temp);
    }
    return sets;
}

The problem is within the highlighted section. When I erase using (*it) erase by value, i get the desired sets. But if I use (it) erase by iterator position which is efficient. I don't get desired results.
Please help me understand what is happening.

A:

std::set<int>::iterator it = iset.begin();

it is an iterator (not a pointer!) that belongs to iset. It's undefined behavior to try and use it with niset.
You could get the behavior you want by doing:
set<int> niset = iset;
std::set<int>::iterator it = niset.begin();
niset.erase(it); // Issue

