0

This is probably an easy question to answer, but I've been trying to change my syntax for using for loops with vectors to be more efficient. I was previously using the normal method for loops:

for (int i = 0; i < vector.size(); i++){
     //code 
}

But am trying to change my approach to the recommended use of iterators

for (const auto i : vector){
     //code 
}

I like this way as it looks cleaner and is obviously the way most people recommend, but the previous method allows me to use "i" so that I can have control of specific element numbers. So if I wanted to access the 5th element I could do that by putting "vector[i]" during the 5th iteration of the loop. I'm sure that the second approach allows for this as well and I'm just ignorant as to the specific code to use. Can someone recommend what they think is the best approach towards getting element access using the second method?

julius72
  • 23
  • 2
  • You can use `for (auto& element : vector){` and then `element` will be a reference to element in the vector (and not a copy) which you can change if you want. – drescherjm Oct 21 '21 at 16:34
  • 9
    One of the main advantages of the range based loop is that indices don't matter. If indices matter to you, it is often simpler to use an index based loop. – 463035818_is_not_an_ai Oct 21 '21 at 16:35
  • Do any of the answers in https://stackoverflow.com/questions/1315041/how-can-i-iterate-through-a-string-and-also-know-the-index-current-position help you? – TheUndeadFish Oct 21 '21 at 16:36
  • this is also very related:https://stackoverflow.com/a/10962575/4117728. Is it an interesting exercise? Certainly yes. Is the most simple solution? meh – 463035818_is_not_an_ai Oct 21 '21 at 16:43
  • 1
    How about this? `for (int& i : list) { std::cout << "index = " << (&i - list.data()); }` – jdt Oct 21 '21 at 17:14
  • 2
    @upkajdt I find that disturbing, you make assumptions on the memory layout of your container. I wouldn't try this for a map, or a set. – Pepijn Kramer Oct 21 '21 at 17:47
  • @PepijnKramer, fortunately, `map` and `set` do not contain a `data` function, so the compiler would stop anyone foolish enough to try that =) – jdt Oct 21 '21 at 18:48

1 Answers1

0

Create a variable before the for loop, and increment it every iteration. Then you can use it as an index.

If you don't want to pollute the outer scope with a new useless variable, you can put it all in additional brackets.

{
   int index = 0;

   for (const auto i : vector){
   //code 
   index++;
   }
}
Dave F.
  • 145
  • 6
  • Your `code` is treating the 1st element as index 1, not 0, so you would need to either 1) initialize `index` to `-1` instead of `0`, or 2) increment `index` after the `code` rather than before it. – Remy Lebeau Oct 21 '21 at 20:22
  • 2
    In any case, `index` can be declared in the loop itself since C++20, thus not polluting the outer scope: `for (int index = 0; const auto i : vector){ ... ++index; }` – Remy Lebeau Oct 21 '21 at 20:29
  • @RemyLebeau that seems like the best solution, would you post it as an separate answer or, should I just edit my own? – Dave F. Oct 22 '21 at 13:47
  • 1
    "*would you post it as an separate answer*" - No need, since it is already described in the linked duplicate. "*or, should I just edit my own?*" - that is up to you. – Remy Lebeau Oct 22 '21 at 15:32