Skip to main content
Filter by
Sorted by
Tagged with
12 votes
2 answers
937 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
151 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
373 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
93 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
116 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
214 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
130 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
173 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
140 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,391
3 votes
2 answers
899 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
237 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
46 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
131 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
-2 votes
1 answer
339 views

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. ...
NPS's user avatar
  • 6,424
1 vote
1 answer
115 views

Can I write a range based for loop in c++ with two parallel iterators I have code like this - class Result{ ... ... }; std::vector<Result> results; std::vector<Result> groundTruth(...
Nitron_707's user avatar
0 votes
0 answers
79 views

I am learning to make the self-defined class compatible with the range-based 'for' loop. Given the range class: class range { private: range_iterator m_begin; range_iterator m_end; public: ...
Xie Qing's user avatar
1 vote
1 answer
1k views

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 ...
4LegsDrivenCat's user avatar
5 votes
1 answer
148 views

I'm trying to iterate over a std::vector<X> contained in a struct T that I access through a std::optional<T>. Contrary to my expectations, the behavior is different if I first store the ...
w128's user avatar
  • 4,978
1 vote
2 answers
468 views

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 ...
HarryP2023's user avatar
0 votes
1 answer
128 views

I have come across the solution to print the 2 D array using C++. The ans was to use auto keyword: for eg: double nut_torques[4][5]; // this is my array with type double for (auto& rows : ...
Chinmay's user avatar
  • 11
3 votes
1 answer
102 views

I was browsing the standard algorithm library and came across an example which used a range based for loop in a way that I had not seen before: https://en.cppreference.com/w/cpp/algorithm/is_heap In ...
HarryP2023's user avatar
0 votes
0 answers
50 views

I just learned range based for loops in c++ and i'm trying to create a function that initializes a vector to all 0's; i did the following: #include <vector> // #include <algorithm> // #...
midi's user avatar
  • 7
2 votes
2 answers
968 views

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 ...
deutschepost's user avatar
3 votes
1 answer
582 views

I would like to drop a number of elements from a map based on some condition: #include <unordered_map> #include <ranges> #include <iostream> int main() { std::unordered_map<...
Stein's user avatar
  • 3,281
-1 votes
1 answer
125 views

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 ...
抹茶抹茶's user avatar
0 votes
1 answer
54 views

I want to make a line like this hello my name sounds very fancy from a vector of these words (std::vector<std::string> myvector = {"hello", "my", "name", "...
user avatar
0 votes
1 answer
115 views

Usage I have two classes, which give me a counter in range based for loops (bit like a simple ranges v3 lib). // Usage with l-values std::initializer_list<int> li = {10, 11, 12, 13, 14}; for (...
Marc's user avatar
  • 414
-4 votes
1 answer
97 views

for (auto& it: map_name) { // __ some _ code __ } I want to know whether using & makes any big difference and can we use it to directly access second element of the iterator?
Shivansh Thapliyal's user avatar
2 votes
0 answers
97 views

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 <vector> #include <set> int ...
Huy Nhat Tran's user avatar
-1 votes
1 answer
68 views

I was curious about why we go through a multidimensional array this way for (auto& row : mat1) { for (int& elements : row) { cin >> elements; } ...
Zoon's user avatar
  • 19
5 votes
1 answer
631 views

The reason for the question is that I've seen code like this: auto fun(std::vector<Foo>&& v) { std::vector<Bar> w; for (auto&& e : v /* not an rvalue, but keep ...
Enlico's user avatar
  • 30.3k
0 votes
1 answer
694 views

I have created a string vector and want to fill it with a "range-based for loop" but it doesn't work. What am I doing wrong? invalid initialization of reference of type ‘int&’ from ...
Fidelity's user avatar
2 votes
1 answer
630 views

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 : ...
Kevin Cui's user avatar
0 votes
0 answers
49 views

(Please let me know if this is a duplicate. I tried to search previous questions but couldn't find the same one.) When structured binding is used in a range-based for loop like this: for (auto & [...
starriet 차주녕's user avatar
1 vote
4 answers
790 views

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 ...
Aiv's user avatar
  • 21
1 vote
1 answer
59 views

The following code is running properly #include<iostream> #include<map> #include<algorithm> #include<string> #include<vector> using namespace std; int main() { std::...
roi_saumon's user avatar
0 votes
2 answers
124 views

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 ...
Chavon4's user avatar
0 votes
3 answers
397 views

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 ...
Trevor Van Loosbroek's user avatar
1 vote
5 answers
851 views

I am trying to add a list to a string. int main() { std::cout << "Hello, welcome to Jay's Coffee!!\n"; std::string name; std::cout <<"What is your name "; std::cin >...
user avatar
3 votes
1 answer
263 views

I'm having a problem with a beginner concept in competitive programming extra space in print may cause wrong answer judgment I want to iterate through a container like map or set but last value should ...
Jee's user avatar
  • 35
-1 votes
1 answer
96 views

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 ...
Allen Ding's user avatar
2 votes
5 answers
344 views

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 <iostream> #include <list&...
kadina's user avatar
  • 5,396
4 votes
1 answer
383 views

I need a vector class that exposes a small subset of the std::vector API. Everything works except range-based for. Here my attempt at implementing a forward iterator, which however does not compile. #...
Joachim W's user avatar
  • 8,457
0 votes
1 answer
999 views

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 -> ...
user avatar
1 vote
1 answer
1k views

I have the following program that iterates through a directory and can catch exceptions: #include <iostream> #include <filesystem> #include <exception> int main() { const std::...
Gary Fisher's user avatar
0 votes
1 answer
624 views

I have the following class MyClass that contains a 2D map (std::map<std::string, std::map<std::string,double>>). I would like to know if it is possible to implement the MyClass::begin() ...
romain.bqt4's user avatar
6 votes
1 answer
119 views

I read this documentation for a range-based for loop: The member interpretation is used if the range type has a member named begin and a member named end. This is done regardless of whether the ...
Ethanabc's user avatar
  • 381
2 votes
3 answers
1k views

I want to go through a given vector of integers and find an integer which the value of the next and previous integer are 0. #include <iostream> #include <vector> using namespace std; int ...
Mo_AminKiaee's user avatar