So I really struggled with this and even now I am not happy with my solution.

I have a `set` that at least contains 0, and may contain other positive `int`s. I need to find the first positive number *not* in the `set`.

So writing a standard `while`-loop to accomplish this is easy.

```
i = foo.begin();
while (i != prev(foo.end()) && *i + 1 == *next(i)){
    ++i;
}
cout << "First number that cannot be formed " << *i + 1 << endl;

```

But when I try to write an STL algorithm version of the loop I get something that fails like this loop:

```
auto i = foo.begin();
while (++i != prev(foo.end()) && *prev(i) + 1 == *i);
cout << "First number that cannot be formed " << *prev(i) + 1 << endl;

```

Both of these loops correctly yield **3** in the case of:

```
set<int> foo{0, 1, 2, 4};

```

But the second loop incorrectly yields 3 instead of **4** in this case:

```
set<int> foo{0, 1, 2, 3};

```

**How can I write this using an STL algorithm and accomplish the behavior of the first loop?**

**EDIT:**

After seeing some of the answers, I'd like to increase the difficulty. What I really want is something that doesn't require temporary variables and can be placed in a `cout` statement.