Questions tagged [range-based-loop]

66 questions
454
votes
8 answers

C++ Loop through Map

I want to iterate through each element in the map without knowing any of its string-int values or keys. What I have so far: void output(map table) { map::iterator it; for (it = table.begin(); it…
NoName
  • 9,824
  • 5
  • 32
  • 52
26
votes
8 answers

Range-based for loop with special case for the first item

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 seems that there ought to be a better…
bpeikes
  • 3,495
  • 9
  • 42
  • 80
14
votes
8 answers

How can I skip elements in a range-based for loop based on 'index'?

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 over the remaining elements. So I'm looking for…
Jay
  • 6,572
  • 3
  • 37
  • 65
9
votes
4 answers

Iterating over odd (even) elements only in a range-based loop

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 elements and increment the index by two: for (int i = 0; i…
Dmytro Dadyka
  • 2,208
  • 5
  • 18
  • 31
7
votes
1 answer

How to pass in a brace-enclosed initializer list to a function?

I want to write a function that can be used with an argument that otherwise could directly occur in a range-based loop: template void sayIt(const Iterable& stuff) { for (const auto& e : stuff) { cout << e << endl; …
shinjin
  • 2,858
  • 5
  • 29
  • 44
6
votes
1 answer

What is member interpretation in Range-based for loop (since C++11)?

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 member is a type, data member, function, or…
Ethanabc
  • 311
  • 2
  • 7
5
votes
1 answer

Why doesn't iterate over a container accessed directly through std::optional::value() work?

I'm trying to iterate over a std::vector contained in a struct T that I access through a std::optional. Contrary to my expectations, the behavior is different if I first store the std::optional into a copy versus if I iterate it…
w128
  • 4,680
  • 7
  • 42
  • 65
5
votes
1 answer

Is there a technical reason why range-based for loop doesn't detect whether it's looping on an rvalue?

The reason for the question is that I've seen code like this: auto fun(std::vector&& v) { std::vector w; for (auto&& e : v /* not an rvalue, but keep reading */) { w.push_back(std::move(e)); } // do stuff with…
Enlico
  • 23,259
  • 6
  • 48
  • 102
4
votes
1 answer

how to write forward iterator using private std::vector base class

I need a vector class that exposes a small subset of the std::vector API. Everything works except range-based for. Here my attempt at implementing a forward iterator, which however does not compile. #include #include template…
Joachim W
  • 7,290
  • 5
  • 31
  • 59
4
votes
6 answers

Is it possible to implement a DefaultIfNull function in C++?

Disclaimer: This is rather more out of curiosity than for a lack of other solutions! Is it possible to implement a function in C++ that: gets passed a pointer of type T either returns a reference-like-thing to the object pointed to by T or, if the…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
3
votes
1 answer

What does the auto "t{1U}" do in the following range-based for loop from std::make_heap example?

I was browsing the standard algorithm library and came across an example which used a range based for loop in a way that I had not seen before: https://en.cppreference.com/w/cpp/algorithm/is_heap In the example given, they use a range based for loop…
HarryP2023
  • 298
  • 1
  • 13
3
votes
1 answer

Deleting map elements in a range-based loop

I would like to drop a number of elements from a map based on some condition: #include #include #include int main() { std::unordered_map numbers = {{1,2}, {2,1}, {3,2}, {4,5}}; auto even =…
Stein
  • 3,179
  • 5
  • 27
  • 51
3
votes
1 answer

Don't print space after last value with iterator

I'm having a problem with a beginner concept in competitive programming extra space in print may cause wrong answer judgment I want to iterate through a container like map or set but last value should not have a space #include #include…
Jee
  • 35
  • 5
3
votes
1 answer

Why can't I use this 'void' type in a range based for loop?

for(auto i: {{1,2,3}, {4,5,6}, {7,8,9}}){ /* loop body */ } I know I have other ways to get my work done. But I was just wondering why we cannot use such type of list in this loop. It is giving me this error: cannot use type 'void' as a range
sparsh goyal
  • 89
  • 1
  • 7
3
votes
3 answers

Is std::filesystem::directory_iterator really an iterator?

Something doesn't make to sense. According to what I've read you use std::filesystem like this: #include #include #include int main() { auto iterator = std::filesystem::directory_iterator("c:/somefolder"); …
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
1
2 3 4 5