I want to use for each loop because it doesn't go to NULL indices(I think) whereas in range for loop it goes through each and every index
Asked
Active
Viewed 31 times
0
-
q.v. [`rbegin`](https://en.cppreference.com/w/cpp/iterator/rbegin) – Eljay Jun 13 '22 at 10:59
-
1what are "NULL indices" ? You can use a loop with (reverse) iterators, there are no "NULL indices" – 463035818_is_not_an_ai Jun 13 '22 at 10:59
-
What are "null indices"? There shouldn't be any major difference between ranged-for and `std::for_each`. – HolyBlackCat Jun 13 '22 at 10:59
-
A for-each loop goes to all the same indices as a loop over the indices in reverse (`for (ssize_t i = vec.size() - 1; i >= 0; --i)`), so your premise is flawed. Iterating a `std::vector
` will get the values that are `NULL` just as it gets the non-`NULL` values. – ShadowRanger Jun 13 '22 at 11:00 -
https://godbolt.org/z/o19sacME4 – Marek R Jun 13 '22 at 11:07
-
thank you for clarifying i thought that if there are 3 indices... lets say 0 1 and 2 ... if 0 and 1 are occupied by a value and 2 is empty then the iterator would skip index 2... but I was wrong :) – Hussain Ahmed Siddiqui Jun 13 '22 at 11:12
-
what do you mean with "2 is empty" ? Vectors have capacity and size. Capacity can be larger than size, but size is the number of elements, and they aren't "NULL" – 463035818_is_not_an_ai Jun 13 '22 at 11:13