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 6109383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:23:55+00:00 2026-05-23T14:23:55+00:00

In the program below I’ve a typedef map . What I want to do

  • 0

In the program below I’ve a typedef map. What I want to do is to implement a hash table. I’m trying to use unordered_map since I heard that is the efficient as it takes O(1) time. I use my typedef map everywhere in my main program (another program that I’m working on) so I don’t want to change that. I want to implement hash table in one of the functions and I’m trying to figure out how to insert the contents of my map into the hash table and search for the key later. I’ve inserted a comment in two places where I’m having trouble. Please help.

#include <iostream>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef vector<int> v_t;
typedef set<int> s_t;
typedef map<s_t, v_t> m_t;
typedef m_t::iterator m_it;
typedef std::unordered_map<s_t, v_t> Mymap;

int main(){
    m_t sample;
    for (int i = 0; i < 100; i = i+2) {
        v_t v;
        for(int k = 100 ; k<=105 ; ++k)
            v.push_back(k);
        s_t k;
        k.insert(i);
        sample.insert(sample.end(), make_pair(k, v));
    }

    //---------Debug--------------------
    for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
        cout << "Key: ";
        copy(it->first.begin(), it->first.end(), ostream_iterator<int>(cout, " "));
        cout << " => Value: ";
        copy (it->second.begin(),it->second.end(),ostream_iterator<double>(cout," "));
        cout << endl;
    }
    //---------------------------------

    Mymap c1;

    for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
        c1.insert(Mymap::value_type(it->first,it->second));   // how to do this ?
    }
    s_t s;
    s.insert(72);
    if(c1.find(s)!=c1.end())                                // does this work ?
        cout << "Success" << endl;
    return 0;
}

I appreciate any help or comments.

After reading Jason’s comments I understand why i cannot use a std::set as a key in unordered_map so I tried to use std::string as a key but the find function won’t work. Could you please help me.

Mymap c1;

for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
    v_t v1;
    std::string key;
    key.insert(key.begin(),it->first.begin(),it->first.end()); 
    copy(it->second.begin(), it->second.end(),std::back_inserter(v1));
    c1.insert(Mymap::value_type(std::make_pair(key,v1)));
}

string s = "72";
if((c1.find(s) != c1.end()) == true) 
    cout << "Success" << endl;
return 0;
  • 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-23T14:23:55+00:00Added an answer on May 23, 2026 at 2:23 pm

    The basic element you’re missing to make this work is to define a hashing function for your std::set that you’re using as the key. The STL already defines equality and lexicographical ordering for a std::set, so you can use it as the key-value in a std::map as-is without any problems. It does not define a hash function though, so that is something you’re going to have to-do by overloading std::hash. This is fairly straight-forward, and can be done by defining the following function:

    namespace std
    {
        template<>
        struct hash<std::set<int> > : public std::unary_function<std::set<int>, size_t>
        {
            size_t operator()(const std::set<int>& my_set) const
            {
               //insert hash algorithm that returns integral type
            }
        };
    }
    

    The above functor object would return an integral type of size_t, and would take a std::set as the argument. You’ll have to define it inside of namespace std so that std::unordered_map will recognize it. An “easy” algorithm could be simply summing the elements since you have a set of type int. There are more complex algorithms out there that would reduce the number of collisions such a simple algorithm would create at the expense of hashing time. Once you have this defined though, you shouldn’t have any problems inserting your std::set key-values into an unordered_map, as well as creating new key-values and finding them in the hash table.

    You can see an example of your source-code working at: http://ideone.com/DZ5jm

    EDIT: Jason’s code placed here for reference:

    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <set>
    #include <map>
    #include <unordered_map>
    
    using namespace std;
    
    namespace std
    {
            template<>
            struct hash<set<int> > : public unary_function<set<int>, size_t>
            {
                size_t operator()(const std::set<int>& my_set) const
                {
                   set<int>::iterator iter = my_set.begin();
                   int total = 0;
    
                   for (; iter != my_set.end(); iter++)
                   {
                       total += *iter;
                   }
    
                   return total;
                }
            };
    }
    
    
    
    typedef vector<int> v_t;
    typedef set<int> s_t;
    typedef map<s_t, v_t> m_t;
    typedef m_t::iterator m_it;
    typedef std::unordered_map<s_t, v_t> Mymap;
    
    int main(){
    
    m_t sample;
    for (int i = 0; i < 100; i = i+2) {
            v_t v;
            for(int k = 100 ; k<=105 ; ++k)
               v.push_back(k);
            s_t k;
            k.insert(i);
            sample.insert(sample.end(), make_pair(k, v));
        }
    
    //---------Debug--------------------
    for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
       cout << "Key: ";
       copy(it->first.begin(), it->first.end(), ostream_iterator<int>(cout, " "));
       cout << " => Value: ";
       copy (it->second.begin(),it->second.end(),ostream_iterator<double>(cout," "));
       cout << endl;
       }
    //---------------------------------
    
    Mymap c1;
    
    for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
       c1.insert(Mymap::value_type(it->first,it->second));   // how to do this ?
       }
    s_t s;
    s.insert(72);
    if(c1.find(s)!=c1.end())                                // does this work ?
       cout << "Success" << endl;
    return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What am I doing wrong in the program below? I want to use std::find()
I have a program below that doesn't seem to be doing what I want
In the program below, there are two things that I don't understand. How can
(In short: main()'s WaitForSingleObject hangs in the program below). I'm trying to write a
Below is my program and I am trying to get the call stack when
In the program below, I am trying to calculate the distance between two points.
I have a program below that tries to take input from the user and
When I press the button in the program below I want the popup menu
I'm trying the program below to divide complex numbers, it works for complex numbers
In the program below, the result is that 0.0 is considered less than Double.MIN_VALUE

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.