Questions tagged [range-based-loop]

66 questions
0
votes
2 answers

C++ Why are my Objects being destroyed, and sometimes twice in a row when using range based for loop

I'm a beginner programmer looking to understand why my objects are being deleted, sometimes even twice. I'm trying to avoid creating them on the heap for this project as that is a more advance topic I will try at a later time. Whats causing the…
Chavon4
  • 5
  • 3
0
votes
3 answers

C++ iterating and range-for-loop differences

I have a chunk of C++ code that is supposed to go through a sorted vector and delete reoccurring objects in-place. I completed the task (C1) using an iterator. I continued with the problem and wanted to do it using range-based for loop like the…
0
votes
1 answer

How to iterate through a range-based for loop when the data type of the array is const char *?

I am trying to solve the following question: https://www.codewars.com/kata/54bf1c2cd5b56cc47f0007a1/train/cpp in C++. When I try to iterate over a range based for loop I get the following error -> In file included from…
user14925305
0
votes
1 answer

Custom class range based for-loop over a 2D map

I have the following class MyClass that contains a 2D map (std::map>). I would like to know if it is possible to implement the MyClass::begin() MyClass::end() functions for MyClass in order to have a…
romain.bqt4
  • 159
  • 1
  • 6
0
votes
2 answers

Why doesn't sorting work for an iterator in Map?

So here is my code; I find the sort doesn't work for the vector in this map. Does anyone know why? The output of this code is still "3 1 2 4 5" map > values; values[1] = {3,1,2,4,5}; for(auto g: values) { sort(g.second.begin(),…
2997ms
  • 26
  • 3
0
votes
1 answer

Dynamically update range inside loop by overload end() method

I want to create a class to store data also dynamically - inside a range based loop. But I face problem to overload end() method. The idea is something like: template class ContainerClass { public: typename std::vector::iterator…
Assen
  • 1
  • 2
0
votes
2 answers

Can you avoid out of range error if you use range based for loop instead of the standard for loop?

I was experimenting around with range-based for loop found that if you use range-based for loop to loop through a vector it runs into out of range error is there any way to avoid this error if you are using range-based for loop int main() { …
0
votes
1 answer

Updating class member values inside a range based for loop in C++

The code on top works. The code below it does not. Why? I am searching through a vector of class objects in C++ using the object's name member so as to increment it's watched member. The regular for loop and syntax successfully increments the…
Seth Van
  • 25
  • 7
0
votes
2 answers

Accessing references to existing object in a range based for loop with structured binding

I am struggling to find an easy solution to alter some already existing objects. Lets assume I have the following pairs std::pair p1 = {1,foo()}; std::pair p2 = {2,foo()}; std::pair p3 = {3,foo()}; with foo being a…
0
votes
4 answers

In a C++ range-based for loop, can begin() return a reference to a non-copyable iterator?

Update: Thanks to everyone who submitted an answer. In short, the answer is that the "iterators" that begin() and end() return must be copyable. Artyer proposed a nice workaround: Make an iterator class that contains a reference (or, alternatively,…
mpb
  • 1,277
  • 15
  • 18
0
votes
2 answers

Difference between for loop and the range based loop in C++

I am confused as to what what the difference is between for loop and for each loop in C++. I read tutorials and some books but I am yet to see how for each loop is different. With arrays, examples I have seen seem to suggest for each loop loops…
Mallam Awal
  • 223
  • 3
  • 9
-1
votes
1 answer

Get a random number when emplace an element with range-based for loop

I wrote a program to insert the numbers in C++ with CLion. First, enter a set of positive integers and place them into the vector called pour. Then, take the negative numbers of these positive integers and then emplace them into the pour. However,…
-1
votes
1 answer

How to make a spaced line of strings from a vector in C++

I want to make a line like this hello my name sounds very fancy from a vector of these words (std::vector myvector = {"hello", "my", "name", "sounds", "very", "fancy"}). What is the most efficient way to perform such convertion without…
-1
votes
1 answer

Multidimensional arrays

I was curious about why we go through a multidimensional array this way for (auto& row : mat1) { for (int& elements : row) { cin >> elements; } } Why do we use &(pass by ref) when we make a temp variable…
Zoon
  • 19
  • 1
-1
votes
1 answer

Why is my range-based for loop working for some data types but not others?

I tried out some range-based for loops to get an idea of the concept and for arrays and vectors of integers, it works fine. But for a vector of characters, my compiler does not give any error messages or the desired result, just a line of red dots.…