range-based-loop Questions

9

Solved

Is there a way to access the iterator (I suppose there's no loop index?) in a C++11 range-based for loop? Often we need to do something special with the first element of a container and iterate ove...
Ashlynashman asked 19/1, 2014 at 11:9

8

Solved

I want to iterate through each element in the map<string, int> without knowing any of its string-int values or keys. What I have so far: void output(map<string, int> table) { map<...
Discophile asked 9/10, 2014 at 15:12

1

Solved

I read this documentation for a range-based for loop: The member interpretation is used if the range type has a member named begin and a member named end. This is done regardless of whether the me...
Miracle asked 26/1, 2022 at 16:18

8

Solved

I find myself often with code that looks like this: bool isFirst = true; for(const auto &item: items) { if(!isFirst) { // Do something } // Normal processing isFirst = false; } It seem...
Apologist asked 22/2, 2019 at 17:6

1

Solved

I want to write a function that can be used with an argument that otherwise could directly occur in a range-based loop: template <typename Iterable> void sayIt(const Iterable& stuff) { f...
Taciturn asked 14/8, 2020 at 5:23

4

Solved

Suppose we have a plain array (or other container which supports range-based loops): const int N = 8; int arr[N] = {0, 1, 2, 3, 4, 5, 6, 7}; Using indexes or iterators, we can loop over odd elemen...
Carhart asked 24/2, 2019 at 9:31
1

© 2022 - 2024 — McMap. All rights reserved.