Looping
One can iterate through all the elements of a set, in increasing
order, thanks to a loop of the form for (const <variable> of <set>) <block>
. It means that the <block>
of statements (or a single
statement) will be computed once for each <variable>
ranging over the
elements of the set <set>
in increasing order.
Here is an example where the integers in a set are summed up.
function sum_elt (s: set<int>) {
let sum = 0;
for (const e of s) sum = sum + e;
return sum;
};
Note: See the predefined namespace Set