Questions tagged [range-based-loop]

66 questions
-1
votes
1 answer

Why is range based for loop error message not still nice as of C++ 20?

In C++20, concepts were added, and so were constrained algorithms such as std::ranges::for_each. Why is the range-based for loop error message has plain errors like: begin and end are not declared in a scope. Why not just: a given expression…
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
-1
votes
1 answer

Difference between range-based for loop and normal for loop Depth-First Search implementation

I'm trying to implement a depth first search algorithm for a graph data structure and my function looks like this: void dfs(int x, vector & Adjacency_List, v_Int& visited) { visited[x] = 1; //Mark current vertex as visited for (int i…
-2
votes
1 answer

Iterating over a vector of shared_ptrs in a range-based for loop

I feel like this should have an answer already but I wasn't able to find it. I have a vector of shared_ptrs: vector> v; I don't want to do anything with ownership (e.g. copying smart_ptrs) and I just want to go over the elements. Is…
NPS
  • 6,003
  • 11
  • 53
  • 90
-2
votes
1 answer

Can't modify the value of a reference in Range based loop

I'm working on a school project of boolean minimization, and here I want to delete some elements of a set of my user defined class. This is where the error occurs: (dc and PI are both sets of my class Term, passed to this function by reference.…
hmlssslmn
  • 11
  • 2
-3
votes
2 answers

Avg of objects c++

I am trying to solve how to get the average of 2-4 objects ideally with the method beeing inside my class. class Student { public: string name; int grades; public: void val(string name, int grades) { this->name = name; …
John
  • 1
-4
votes
1 answer

What is the difference in using and not using "&" in range-based for loops usually iterating over maps in C++?

for (auto& it: map_name) { // __ some _ code __ } I want to know whether using & makes any big difference and can we use it to directly access second element of the iterator?
1 2 3 4
5