Questions tagged [ranged-loops]

Nowadays, almost every programming language has a convenient way to write a for loop over a range of values. Finally, C++ has the same concept; you can provide a container to your for loop, and it will iterate over it.

Similar to the java "for each" loop. Nowadays, almost every programming language has a convenient way to write a for loop over a range of values. Finally, C++ has the same concept; you can provide a container to your for loop, and it will iterate over it.

64 questions
401
votes
10 answers

C++11 reverse range-based for-loop

Is there a container adapter that would reverse the direction of iterators so I can iterate over a container in reverse with range-based for-loop? With explicit iterators I would convert this: for (auto i = c.begin(); i != c.end(); ++i) { ... into…
Alex B
  • 82,554
  • 44
  • 203
  • 280
113
votes
8 answers

Is there a range class in C++11 for use with range based for loops?

I found myself writing this just a bit ago: template class range_class { public: class iterator { friend class range_class; public: long int operator *() const { return i_; } const…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
14
votes
8 answers

Error with ranged for inside function

I'm having a little bit of trouble with the ranged for in C++. I'm trying to used it to display the element on and int array (int[]) and it works completely fine when I do that on the main function, like in: int main(int argc, char const *argv[])…
Pj-
  • 430
  • 4
  • 14
13
votes
2 answers

Obtaining item index in ranged based for on vector

The C++11 introduced ranged-based for loop that is internally implemented using (const) iterators so this: std::vector vec; for(std::string &str : vec) { //... } is basically equivalent to more verbose (yes, it could be simplified…
Resurrection
  • 3,916
  • 2
  • 34
  • 56
10
votes
1 answer

vc++ no longer vectorize simple for loops with range-based syntax

Before replacing a lot of my "old" for loops with range based for loops, I ran some test with visual studio 2013: std::vector numbers; for (int i = 0; i < 50; ++i) numbers.push_back(i); int sum = 0; //vectorization for (auto number =…
ThreeStarProgrammer57
  • 2,906
  • 2
  • 16
  • 24
9
votes
1 answer

using shared_ptr to std::vector in range-based for loop

I wrote a c++ function that assembles some data then then returns a std::shared_ptr to a newly allocated std::vector containing the data. Something analogous to this: std::shared_ptr> shared_ptr_to_std_vector_of_ints() { auto v…
Craig Reynolds
  • 675
  • 7
  • 16
7
votes
1 answer

Is there a concept in the standard library that tests for usability in ranged for loops

There are a number of different ways, that make a type/class usable in a ranged for loop. An overview is for example given on cppreference: range-expression is evaluated to determine the sequence or range to iterate. Each element of the sequence,…
Jakob Stark
  • 3,346
  • 6
  • 22
7
votes
4 answers

How do I extend the lifetime of a temporary in a ranged for expression?

I'm getting dangling references while using a ranged-for loop. Consider the following C++14 expression (full example program below): for(auto& wheel: Bike().wheels_reference()) wheel.inflate(); It's output is: Wheel() Wheel() …
Remco
  • 260
  • 1
  • 7
5
votes
2 answers

How do I iterate an mdspan?

So, I decided I want to use mdspan's rather than a span + element access function. But - one obvious thing one would want to do with an (md)span is iterate its elements. This works for spans: std::vector vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
4
votes
1 answer

ranged for loop of variables results in returning address-reference of local variable?

// g++ --std=c++17 test.cpp -I /usr/local/include -L /usr/local/lib -lboost_system -Wall -pedantic -Wreturn-type -Wstrict-aliasing -Wreturn-local-addr -fsanitize=address -g // LD_LIBRARY_PATH=/usr/local/lib ./a.out #include #include…
inetknght
  • 4,300
  • 1
  • 26
  • 52
4
votes
3 answers

C++ basics: ranged based for-loop and passing C-style arrays to functions

I'm trying to learn C++ but I just can't wrap my head around this code here, despite spending a good amount of time searching for an answer: #include void printArray1(int (&array)[3]) { for(int x : array) std::cout << x << "…
Joey
  • 43
  • 1
  • 4
4
votes
4 answers

Range based for loops and multiple iterators

I have the following code, representing a mesh in a 3D application (some code ommited for clarity): class Mesh { public: typedef std::vector Vertices; typedef std::vector Elements; template
Morgan Bengtsson
  • 1,341
  • 1
  • 14
  • 23
3
votes
3 answers

C++ ranged vs manual for loop

I am new to C++ and for practice I have been solving some problems on projecteuler.net. One of the questions involves analyzing a 1000 digit number, so I wrote a program that can read in the number and stores it in a vector. In order to test my…
nubilot
  • 83
  • 3
3
votes
3 answers

Is it Possible to Use Casting as "array slicing" in C++11

I have some shared memory populated by specialized hardware. It's declared as an array of structs, like: struct port { int data[10]; char port_id[8]; } struct bus { port ports[5]; char bus_id[8]; } struct bus busses[10]; I'm…
3
votes
1 answer

Wrap iteration handle for use in range-based for-loops

I use an API that comes with an iteration functionality using a void* handle. void* handle = BrowseInit(); while (BrowseGetNext(handle)) { // ... int x = BrowseGetData(handle); } BrowseFree(handle); How would I go about wrapping this into a…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
1
2 3 4 5