Skip to main content
12 votes
2 answers
1k views

It is easy to iterate std::map and update values: #include <iostream> #include <map> int main() { std::map<int, int> m; m[1] = 2; m[2] = 4; for (auto &[k, v] : m)...
Fyodor Menshikov's user avatar
2 votes
2 answers
160 views

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(...
Adam Badura's user avatar
  • 5,533
6 votes
1 answer
420 views

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 ...
Artem Selivanov's user avatar
0 votes
3 answers
94 views

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 ...
Newline's user avatar
  • 993
2 votes
1 answer
150 views

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 &...
Fei Yu's user avatar
  • 65
2 votes
2 answers
118 views

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 ...
Darko Veberic's user avatar
1 vote
1 answer
236 views

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 &&...
riverofwind's user avatar
0 votes
1 answer
132 views

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 ...
riverofwind's user avatar
0 votes
2 answers
175 views

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 ...
eklund's user avatar
  • 3
3 votes
1 answer
144 views

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. ...
Dr. Gut's user avatar
  • 3,497
3 votes
2 answers
930 views

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"...
Leon's user avatar
  • 2,165
3 votes
2 answers
249 views

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 ...
bradgonesurfing's user avatar
0 votes
0 answers
47 views

**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; ...
Abhinav's user avatar
  • 19
0 votes
1 answer
119 views

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()(...
Ali Tavakol's user avatar
0 votes
2 answers
133 views

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 ...
Emiliano Toledo's user avatar

15 30 50 per page
1
2 3 4 5 6