Questions tagged [for-range]

16 questions
16
votes
2 answers

Range-based for loop on a temporary range

Thanks to some segmentation faults and warnings in valgrind, I found that this code is incorrect and has some sort of dangling reference in the for-range loop. #include #include auto f(){ std::vector> v(10,…
alfC
  • 14,261
  • 4
  • 67
  • 118
6
votes
1 answer

Map in order range loop

I'm looking for a definitive way to range over a Go map in-order. Golang spec states the following: The iteration order over maps is not specified and is not guaranteed to be the same from one iteration to the next. If map entries that have not yet…
fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
5
votes
3 answers

range-based for loops in c++

It would seem that the "for each" style of syntax available in C++11 permits array iteration without knowledge of the actual size of the array (number of elements). I assume, since it is part of the new standard, that this is perfectly safe, even…
johnbakers
  • 24,158
  • 24
  • 130
  • 258
4
votes
0 answers

Is clang always right in warning me to preventing copying?

Consider the following code: std::vector> v; for (const auto &p : v) { // ... } for (const auto &[first, second] : v) { // ... } There is a vector of pairs of chars and two ways to iterate over the elements using range…
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
4
votes
1 answer

inserting into the back of std::list during range based for loop

I came across "Add elements to a vector during range-based loop c++11", and wasn't surprised it's not allowed using std::vector because the append can invalidate iterators. However, inserting into std::list doesn't invalidate any of the iterators,…
Daniel McIntosh
  • 522
  • 3
  • 17
3
votes
1 answer

Go to next iterator in range-based for loop

for my project I need to make the iterators from the loop to go to the next item in the container, do some operations, and return again back to the same iterator and just continue, however, for some reason neither advance nor next and then using…
Nikola
  • 133
  • 9
2
votes
1 answer

Literal for ranges in C++11

If I have this: for (auto iSong = 1; iSong <= iMaxSongNumber; iSong++) Can I use the new for range approach? I understand that for containers they need a begin and end method for them to work. But if we have literal max values?
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
1
vote
1 answer

Why is this const auto variable in a range-for loop compiling for a const member function of a class?

I have the following classes declaration, and according to what I've learned related to const member functions, const objects can not call non-const member functions. In the range-for loop, we are using "const auto animal", which is supposely using…
learning_dude
  • 1,030
  • 1
  • 5
  • 10
1
vote
0 answers

Range-based loop or for_each with execution policy better?

I've read about those new execution policies in C++17 on cppreference.com: https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t And I was wondering should we prefer those now over range-based loops if we want to allow the compiler to…
Michael Mahn
  • 737
  • 4
  • 11
0
votes
2 answers

standard unicode in GO programming language

I have a string containing cards from ace of hearts to 10 of hearts via unicode (the exercise requires using a string, so no arrays or slices) Given a number n I have to extract n cards from this string. How do I do this if with the for-range I get…
0
votes
0 answers

boost counting_range doesn't include iterating the last value

I have this code: void CMeetingScheduleAssistantApp::InitDateTransArrays() { if (theApp.UseTranslationINI() || theApp.GetProgramLanguage() == LANGUAGE_MALAGASY) { CString strKey; // Days of the week for (auto const…
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
-1
votes
1 answer

for loop - java and Python work differently

I want to modify variable y and it works in Java as below for(int x=0;x<4;x++) { for(int y=0;y<3;y++) { System.out.print(y); if(y==1){y+=1;} } }…
Ke.N
  • 19
  • 5
-1
votes
1 answer

could someone explain that C++ code for me?

In the following code what is the meaning of colon? and where callback function came from? using void_callback_f = void (*)(); std::vector _reload_callbacks; void Reload() { for (const auto& callback : _reload_callbacks) { …
-2
votes
2 answers

C++ VS Code on OSX forrange loop

I'm stuck with forrange loop in VS Code. It gives me error: expected a ';' expected an expression VS Code C++ error mp[0] = 10; mp[1] = 200; mp[2] = 3000; mp[3] = 40000; for (int id : mp) // error for ":" and ")" { …
Sanya
  • 1
  • 2
-2
votes
1 answer

Range over string slice is inconsistent

This code: import "fmt" import "time" func main() { string_slice:=[]string{"a","b","c"} for _,s:=range string_slice{ go func(){ time.Sleep(1*time.Second) fmt.Println(s) }() } …
1
2