In this post, we will see how to resolve What does the for loop do here? Question: I’m trying to understand this code block, I saw this in class and I still don’t really get it. I understand what and ...

Question: A recent talk about unordered_map in C++ made me realize that I should use unordered_map for most cases where I used map before, because of the efficiency of lookup ( amortized O(1) vs. O(log n) ). Most times I ...

Question: I am writing a simple logging class with C++. I want to create an unordered_map to store logging_levels, but compiling fails. Answer: You can’t put arbitrary code wherever you want in a class definition. Initialize the hash table in ...

Question: I am studying the Rule of Five and it’s cousins (Rule of Four and 1/2, Copy and Swap idiom, Friend Swap Function). I implemented the Rule of Four and 1/2 on a test class. It compiles well. Is there ...

Question: I have this code: It compiles fine. But produces a Segmentation fault upon executing. Why can’t I find the distance between iterators? Answer: Your code has undefined behavior. umap is empty, then std::next(itr1, 3) returns an invalid iterator. Change ...

Question: I’ve defined a structure like this: I’m implementing a map of map like this: Upon using the below code. I’m receiving error error: no matching function for call to 'Node::Node()' second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...) Please help out why I’m receiving error. I’ve ...

Question: I have a unordered map std::unordered_map<void (MyClass::*)(), double> used to store the time that has elapsed since a function has been called. I’m getting an error saying that error: use of deleted function ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map() [with ...