Building a set of all subsets recursively in C++ Here is my recursive implementation of a power set function in C++.  One thing that concerns me is that I am iterating over the set of sets while modifying its size. The code nevertheless functions as desired. Any feedback would be highly appreciated.

#include &lt;iostream&gt;
#include &lt;set&gt;
#include &lt;vector&gt;

template &lt;typename T&gt;
void power_set(int i, const std::vector&lt;T&gt;&amp; set, std::set&lt;std::set&lt;T&gt;&gt;&amp; set_of_sets)
{
    if (i == set.size())
    {
        set_of_sets.emplace(std::set&lt;T&gt;());  // empty set
        return;
    }

    power_set(i + 1, set, set_of_sets);

    for (auto&amp; subset : set_of_sets)
    {
        auto augmented_subset = subset;
        augmented_subset.insert(set[i]);
        set_of_sets.insert(augmented_subset);
    }

}

int main()
{
    std::vector&lt;std::string&gt; set = {"a", "b", "c"};
    std::set&lt;std::set&lt;std::string&gt;&gt; set_of_sets;
    power_set(0, set, set_of_sets);

    for (auto&amp; subset : set_of_sets)
    {
        std::cout &lt;&lt; "{";
        for (auto&amp; i : subset) std::cout &lt;&lt; i &lt;&lt; ",";
        std::cout &lt;&lt; "}\n";
    }
    std::cout &lt;&lt; set_of_sets.size() &lt;&lt; "\n";
}
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;
}