Questions tagged [range-based-loop]

66 questions
2
votes
2 answers

How to iterate over a list of smart pointers?

In Herb Sutter's 2014 CppCon talk, he talks about how you shouldn't have smart pointers in your function declaration if you don't intend to transfer or share ownership. And most definitely, no const references to smart pointers. If I have a function…
2
votes
0 answers

Why range based for loop doesn't work with iterator, which points to container?

I have this code and it doesn't compile. It has an error: cannot assign to variable 'value' with const-qualified type 'const int &' value = 5; #include #include int main() { std::vector> data; for (auto it =…
Huy Nhat Tran
  • 135
  • 11
2
votes
1 answer

range-expression of range based for loop in C++

I am trying to pass a pointer of vector to range based for loop for its range-expression. Here is the syntax of range based for loop: attr(optional) for ( init-statement(optional) range-declaration : range-expression ) loop-statement Referenced…
Kevin Cui
  • 35
  • 5
2
votes
5 answers

using remove method of std::list in a loop is creating segmentation fault

I am using remove() of std::list to remove elements in a for loop. But it is creating segmentation fault. I am not using iterators. Program is given below. #include #include using namespace std; int main() { list li =…
kadina
  • 5,042
  • 4
  • 42
  • 83
2
votes
2 answers

C++ vector prints out weird elements

I'm currently in the process of learning C++ and for that I'm reading the book "C++ Primer". The book is pretty good so far and I have learned a lot however I experienced weird behaviour using a vector and I'm unsure if this is right or if it's a…
anon
2
votes
1 answer

Doesn't for( auto && v : arr ) clobber the contents of the array?

In these questions (also here) there is recommendation to write code like: for( auto&& v : arr ) But doesn't moving the data from arr into v clobber the array arr? How can this style of loop be safe at all?
bobobobo
  • 64,917
  • 62
  • 258
  • 363
2
votes
1 answer

What is different between two codes of for loop?

#include #include using namespace std; int main(void) { vector a = {1, 2, 3, 4, 5}; for (auto &x : a) cout << x << endl; } #include #include using namespace std; int main(void) { …
Joonkyo
  • 23
  • 2
2
votes
1 answer

Why can I bind rvalues to non const reference with a for loop in C++?

The logic of the following is confusing me. It compiles ok but the result of foo is an rvalue. It is a temporary. Why am I then allowed to get non constant references to items in the container? https://godbolt.org/z/vEafrE #include…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
2
votes
1 answer

What's the benefit of range based for loop over traditional c style for loop?

I'm implementing my own programming language and getting stumped on what for loop syntax to choose. It seems that all latest and cool languages are using the range based for loop. I googled hard in order to understand its pros and cons, the the only…
Bill Yan
  • 3,369
  • 4
  • 27
  • 42
2
votes
1 answer

How to change C++98 mode in DEV C++ to C++17 mode that supports range based for loops?

I have been using range based for loops for quite a time.I am doing it by changing the settings of my Dev C++ compiler from this way.However new features of c++ 17 won't support in this compiler.Like for (auto&& [first,second] : mymap) { // use…
Perdente
  • 75
  • 2
  • 11
2
votes
2 answers

How to count the number of a given VALUE(!) in a C++ map?

I do have a cpp class in an otherwise iOS/ObjC project. It uses the following map: std::map testMap; I do know that I can "count" the number of appearances of a given key in that map via testMap.count. But how do I count the…
McMini
  • 305
  • 3
  • 15
1
vote
1 answer

Multi variable range based for loop c++

Can I write a range based for loop in c++ with two parallel iterators I have code like this - class Result{ ... ... }; std::vector results; std::vector groundTruth(2); int gtIndex = 0; for(auto& r:results){ auto gt =…
Nitron_707
  • 307
  • 4
  • 10
1
vote
1 answer

Structured binding with map in range-based loop captures by reference instead of value

In the code below I use auto (not auto&, const auto, const auto& or auto&&), but k has a reference type. Why is it captured by reference (as GCC says) and not by value? Looks counterintuitive. If it's captured by value (as Clang says), why it is…
4LegsDrivenCat
  • 1,247
  • 1
  • 15
  • 24
1
vote
2 answers

Range for loop for empty initializer list

I was reading about forwarding references on cpp reference https://en.cppreference.com/w/cpp/language/reference#Forwarding_references and I was interested to learn that there is a special case for forwarding references: auto&& z = {1, 2, 3}; //…
1
vote
4 answers

How can I add up the values of the individual digits in a string?

I want to get the sum of the individual digits of an ID entered by the user. So far this is the code I have, and my code can count the number of characters in the user input but I'd like for it to also calculate the sum of the individual digits. //…
Aiv
  • 21
  • 1