Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:12:24+00:00 2026-05-27T16:12:24+00:00

My declaration was unordered_map<Comparison,int> Chs when Comparison is a class name. Now I have

  • 0

My declaration was unordered_map<Comparison,int> Chs when Comparison is a class name.
Now I have few questions;

A. How can I insert, when the constructor called, some elements (like done with vector) ?

e.g. :

unordered_map<Comparison,int> Chs =
{
    (new Equal_to<int>,10),
    (new Not_equal_to<int>,30),        
    (new Greater<int>,20)
};

this code get now compile error.

B. How to free the memory that allocated (with new) in this declaration?

C. In inherit class:

template <class T,class V>
class RC : public unordered_map<T, V>

How should I write the RC constructor, so its can initialize with elements like A question?

Thank you, and sorry about my poor English.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:12:25+00:00Added an answer on May 27, 2026 at 4:12 pm

    You’d rather not inherit from standard containers, they weren’t designed for it.

    Also, as mentioned, to have the contained Comparisons behave polymorphically, you need to either store references or pointers. Pointers are much simpler to work with.

    In order to avoid having to worry with freeing up the memory, use smart pointers (std::unique_ptr or std::shared_ptr).

    The following snippet shows the workings of most of the above, including how to do uniform initialization on your own container class (RC<>):

    #include <unordered_map>
    #include <memory>
    
    struct Comparison {};
    template <class T> struct Equal_to     : Comparison { };
    template <class T> struct Not_equal_to : Comparison { };
    template <class T> struct Greater      : Comparison { };
    
    template <class T,class V>
    class RC
    {
        typedef std::unordered_map<T,V> comps_t;
        typedef typename comps_t::value_type value_type;
        comps_t comps;
    
      public:
        RC(std::initializer_list<value_type> init) : comps(init) { }
    };
    
    int main(int argc, const char *argv[])
    {
        RC<std::shared_ptr<Comparison>, int> demo = { 
            { std::make_shared<Equal_to<int>>(),10 },
            { std::make_shared<Not_equal_to<int>>(),30 },        
            { std::make_shared<Greater<int>>(),20 } };
    
        return 0;
    }
    

    In the department of (likely useless optimization, you can have a perfect forwarding constructor if it doesn’t conflict with your other constructor needs. This would have the benefit of supporting move semantics correctly:

    template <typename... A>
        RC(A&&... a) : comps(std::forward<A...>(a...)) { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have menu with few unordered list items as shown below. Now I need
Class declaration: class unaryOperators { public: int i; unaryOperators (int tempI = 0) {
Declaration;) MKMapView mapView; I can't found the difference between mapView.userLocation.location.coordinate and mapView.userLocation.coordinate. Is there
Given a declaration like this: class A { public: void Foo() const; }; What
A using declaration does not seem to work with an enum type: class Sample{
Given this C API declaration how would it be imported to C#? int _stdcall
I have a basic unordered list in HTML/CSS as follows... <div class=floatleft> <ul class=help>
Regarding class public class declaration, please look at these two pieces of code: public
I have a Tree class of the following type class Tree{ private: Node *root;
I have been struggling with a problem for a couple of days now with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.