In the first, iterating through the elements in the container is managed by the compiler. In the second, you use i, however i is not sufficient for this purpose.
An equivalent loop might look more like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for (std::size_t index = 0, i = 0; index < elements.size(); ++index)
{
auto it = existing_elements.find(elements[index]);
if (it == existing_elements.end())
{
// do stuff using value of i here
++i;
}
else
{
// do stuff
}
}
Note that the use of index and i are not interchangeable.
Thanks. I think I get it now. I was only increasing i on some of the loop iterations so it didn't work to use it as the looping variable.
Here is what I have now. It only increases the unique index after finding a unique element, but relies on the i variable to iterate through the entire container.