Questions tagged [range-based-loop]

66 questions
1
vote
1 answer

Range based for loop iteration on std::map need for explanation

The following code is running properly #include #include #include #include #include using namespace std; int main() { std::map m; m[0] = "hello"; m[4] = "!"; m[2] =…
roi_saumon
  • 489
  • 4
  • 13
1
vote
5 answers

How to add a list to a string?

I am trying to add a list to a string. int main() { std::cout << "Hello, welcome to Jay's Coffee!!\n"; std::string name; std::cout <<"What is your name "; std::cin >> name; std::cout <<"Hello " << name << ", thank you so much for coming in…
user18326065
1
vote
1 answer

Continue in range based for loop after exception

I have the following program that iterates through a directory and can catch exceptions: #include #include #include int main() { const std::filesystem::path md("/"); try { for(auto const& dir :…
1
vote
1 answer

Why is this working in a normal for loop but not a range based for loop?

void set_fee(Patron p, int fee) { for (Patron x : patrons) { if (p.get_name() == x.get_name()) x.set_fee(fee); } for (int i = 0; i < patrons.size(); i++) { if (patrons[i].get_name() == p.get_name())…
yaelreshet11
  • 131
  • 5
1
vote
1 answer

How to take inputs using "range based for loop"

The loop containing "cin" doesn't change the values. What is happening inside the loop? And how can I use this(range based loop) to take inputs? INPUT:- #include using namespace std; int main() { int arr[]={1,2,3,4,5}; //with…
Pranshu_Taneja
  • 186
  • 1
  • 1
  • 16
1
vote
1 answer

What is the difference between range based loop and stringstream while extracting characters from a string

consider a string like string s = "xyz123". for(char ch : s) cout<>ch) cout<
1
vote
7 answers

Problem using iterators with dynamically allocated array

I have a variable k of type int to set the length of a dynamically allocated int array: int *Numbers = new int[k]; But because of this I cannot iterate over the array, I get an error: "no matching begin function was found required for this…
undefined
  • 11
  • 4
1
vote
3 answers

Auto reference for vectors?

The following code will not alter the contents of i in the for loop: class Solution { public: vector> combinationSum(vector& candidates, int target) { if (target == 0) {return vector>{{}};} else if…
bayesianpower
  • 93
  • 2
  • 8
0
votes
2 answers

Usage of std::span with non-iterable type

I was trying out some C++20 when I stumbled upon a rather strange (at least to me) situation. I was expecting the following code to not work with one of my examples (I've specified the expected behavior with comments), but it did work, and I want to…
0
votes
0 answers

How to make "auto" type deduction compatible with range-based "for"?

I am learning to make the self-defined class compatible with the range-based 'for' loop. Given the range class: class range { private: range_iterator m_begin; range_iterator m_end; public: range() : m_begin(0), …
Xie Qing
  • 81
  • 7
0
votes
1 answer

Printing 2D array with forEach in C++, but without using auto keyword

I have come across the solution to print the 2 D array using C++. The ans was to use auto keyword: for eg: double nut_torques[4][5]; // this is my array with type double for (auto& rows : nut_torques) { for (auto cols : rows) { …
0
votes
0 answers

Range based for loop in function as reference throws error (C++)

I just learned range based for loops in c++ and i'm trying to create a function that initializes a vector to all 0's; i did the following: #include // #include // #include using namespace std; void…
0
votes
1 answer

r-value lifetime issue (stack-use-after-scope): How to move std::initializer_list

Usage I have two classes, which give me a counter in range based for loops (bit like a simple ranges v3 lib). // Usage with l-values std::initializer_list li = {10, 11, 12, 13, 14}; for (auto [value, index] : enumerate(li)) std::cout <<…
Marc
  • 338
  • 2
  • 15
0
votes
1 answer

how to tell compiler to do "range based for loop" in string vector?

I have created a string vector and want to fill it with a "range-based for loop" but it doesn't work. What am I doing wrong? invalid initialization of reference of type ‘int&’ from expression of type ‘std::__cxx11::basic_string’ Does the…
Fidelity
  • 33
  • 4
0
votes
0 answers

Difference between auto& and auto&& in structured binding used in range-based for loop

(Please let me know if this is a duplicate. I tried to search previous questions but couldn't find the same one.) When structured binding is used in a range-based for loop like this: for (auto & [k, v] : myMap) {...} or for (auto && [k, v] : myMap)…
starriet
  • 2,565
  • 22
  • 23