Building a set of all subsets recursively in C++
The recursion - even simulated via std::stack - is indeed unnecessary. Each subset has a corresponding number in \$[0, 2^n)\$ with bits indicating presence of an element in the subset, and each number in \$[0, 2^n)\$ corresponds to a subset. Consider a pseudocode

Set power_set(Set&amp; the_set)
{
    Set result;
    Integral n = the_set.size()
    for (i = 0; i &lt; (1 &lt;&lt; n); i++) {
        result.insert(make_subset(the_set, i);
    }
    return result;
}

Set make_subset(Set&amp; the_set, Integral indicator)
{
    Set result;
    Integral n = the_set.size();
    for (i = 0; i &lt; (1 &lt;&lt; n); i &lt;&lt;= 1) {
        if (indicator &amp; i) {
            result.insert(the_set[i]);
        }
    }
    return result;
}