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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:15:48+00:00 2026-05-24T06:15:48+00:00

I want to use the boost map and the documentation says I need an

  • 0

I want to use the boost map and the documentation says I need an equality function and a hash function. I think understand what they should do but since I can’t find any examples I’m not sure how to do it so I am looking for a trivial example, like a point class with members x, y or something close.

Edit: Finally got it working. Wish I hadn’t had to waste so much time for this. Thanks anyway guys.

#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <boost/foreach.hpp>
#include <iostream>

namespace test { // class whose source i can't edit
    class point
    {
    public:
        float x;
        float y;
        point() : x(0), y(0) {}
        point(int x, int y) : x(x), y(y) {}
        point(float x, float y) : x(x), y(y) {}
        point(double x, double y) : x((float) x), y((float) y) {}

        bool operator==(point const& other) const
        {
            return x == other.x && y == other.y;
        }
    };
}

namespace test { // my source file
    std::size_t hash_value(point const &p) {
        boost::hash<int> hasher;
        return hasher(p.x) + hasher(p.y);
    }
} 

int main() {
    boost::unordered_map<test::point, std::string> myMap;
    test::point p1(1, 2);
    myMap[p1] = "1"; //now it works
    std::cout << myMap[p1] << std::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-24T06:15:50+00:00Added an answer on May 24, 2026 at 6:15 am

    Equality and hash aren’t too tough to define. Equality:

    class Point {
        int x, y;
        bool operator==(const Point& p) {
            return (x == p.x && y == p.y);
        }
    };
    

    Hashing tends to involve specializing a function or class.

    template<> class boost::hash<Point> {
    public:
        size_t operator()(const Point& p) {
            return boost::hash<int>(p.x) + boost::hash<int>(p.y);
        }
    };
    

    You may need to read up on the specifics of your hash_map implementation for details, and you may also want to define a different hash algorithm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use boost::thread_specific_ptr but need to know what to add to my
I want to use boost::crc so that it works exactly like PHP's crc32() function.
We use Boost statically linked with our app but now I want to use
I want to use boost::asio but I don't want boost to throw exceptions, because
I want to use boost::bind to create a boost::function inserting a new key-value pair
I want to use a boost::function and pass it to a function to function
I have to use boost::spirit for parsing, and I want use phrase_parse function :
I want to use boost::any to store heterogeneous function pointers. I get an exception
for my application, i need to use a hash map, so i have written
I want to use boost::timed_wait to wait for an event or to timeout after

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.