79 questions
2 votes
2 answers
141 views
Structured binding for std::views::enumerate of std::map [duplicate]
I'm trying to do something like this: const std::map<char, int> histogram{ {'A', 2}, {'D', 1}, {'M', 1}, }; for (const auto& [index, key, value] : std::views::enumerate(...
6 votes
1 answer
347 views
How does `template for` iteration work in C++26? [duplicate]
I'm experimenting with the new C++ compile-time reflection features (as described in P2996R0) and I testing a simple enum_to_string() utility using template for: template <typename E> requires ...
0 votes
3 answers
93 views
C++ - Range based loop and namespaces [duplicate]
I've read that to make one's custom container work with range-based for loop, "things" need to be in the same namespace. What things need to be in same namespace? The begin-end free ...
2 votes
1 answer
146 views
What would happen if I append elements in range based for? [duplicate]
I want to modify (push_back) element while iterate vector like this: auto main() -> int { std::vector<double> v { 1, 2, 3 }; for (auto& num : v) { std::cout << num &...
2 votes
2 answers
109 views
get lines from a ifstream into strings in a range-based for loop
I use to parse lines from a file by writing, ifstream input(filename); for (string line; getline(input, line); /**/) do_something_with(line); ... but is it possible to elegantly (or with some boost ...
1 vote
1 answer
201 views
C++ Does Ranged-Based For Loop Use RValue Reference? [duplicate]
Hi I have a quick question - over here it says ranged-based for loops of the form for ( init-statement (optional) range-declaration : range-expression ) are equivalent to the code: { auto &&...
0 votes
1 answer
128 views
C++ Qt best way to loop over a vector that is a member of a vector of parent dummy objects also being looped over
I want to loop over a vector of objects each containing another vector of objects to be looped over in turn. The subvector/contained vector object members need to be able to be able to be modified and ...
0 votes
2 answers
173 views
Range of enums in C++
I'm confused with range of enums. I have code like this: namespace regs { enum Control_0 { THING1, THING2, THING3 }; enum Control_1 { THING100, THING200, THING300 }; const auto ...
3 votes
1 answer
136 views
Range-based for loop: why is ordinary lookup not performed?
In range-based for when begin-expr and end-expr are begin(range) and end(range), the names begin and end are not looked up using ordinary lookup. Just ADL is performed. See C++23 [stmt.ranged]#1.3.3. ...
3 votes
2 answers
874 views
Erasing nodes of a std::map within a range-based "for" loop
I need to check data of a std::map, and remove some of them. I'm using a range-based "for" loop, like this: std::map<int, string> data { { 1, "name1" }, { 2, "name2"...
3 votes
2 answers
231 views
A c++20 range adaptor whose produced values are iterators to the underlying range
I want to have the following work std::vector<int> range{0,1,2,3}; for(std::vector<int>::iterator vit : range | iterator_range ){ int v = *vit; } this would be equivalent to the ...
0 votes
0 answers
45 views
range - based loop traversal in array [duplicate]
**The code I tried to implement is : ** #include<iostream> using namespace std; int main() { int n, k; cout << "Enter the number of elements: \n"; cin >> n; ...
0 votes
1 answer
117 views
Why is iterating over the set and modifying element not allowed here [duplicate]
In the following code, we cannot iterate over set s1 using non-const reference. Why? #include <set> struct st { unsigned int f; std::set<int> s2; }; struct comp { bool operator()(...
0 votes
2 answers
125 views
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 ...
-2 votes
1 answer
327 views
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<shared_ptr<X>> v; I don't want to do anything with ownership (e.g. ...