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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:41:30+00:00 2026-06-18T16:41:30+00:00

The following code defines a unordered_set . The code compiles just fine. But using

  • 0

The following code defines a unordered_set.
The code compiles just fine.
But using the lambda function instead of functor throw when calling find:

libc++abi.dylib: terminate called throwing an exception

#include <unordered_set>

class pair_hash {
    public:
        size_t operator() (const std::pair<int, int> &x) const {
            return std::hash<int>()(x.first) ^ std::hash<int>()(x.second);
        }
};

int main() {
    std::unordered_set<std::pair<int, int>, pair_hash> temp;
    temp.find(std::make_pair(0,0));


    std::function<std::size_t(std::pair<int , int>)> fpair_hash;
    fpair_hash = [](const std::pair<int, int>& v) -> std::size_t
    {
        return std::hash<int>()(v.first) ^ std::hash<int>()(v.second);
    };

    std::unordered_set<std::pair<int, int>, decltype(fpair_hash)> temp2;
    //why does this not work?
    temp2.find(std::make_pair(0,0));
    return 0;
}

clang++ -std=c++11 -stdlib=libc++ -o test test.cpp

  • 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-06-18T16:41:31+00:00Added an answer on June 18, 2026 at 4:41 pm

    decltype(fpair_hash) is std::function<std::size_t(std::pair<int , int>)> so you are just building set with empty hash function.

    You need to provide your function to constructor of std::unordered_set:

    std::unordered_set<std::pair<int, int>, decltype(fpair_hash)> temp2(10, fpair_hash);
    

    This should make it work, but using std::function will have overhead of polymorphic call and you probably don’t need it:

    auto fpair_hash = [](const std::pair<int, int>& v) -> std::size_t
    {
        return std::hash<int>()(v.first) ^ std::hash<int>()(v.second);
    };
    

    Finally, your hash function is not really good – it maps all pairs (x, x) to 0. Perhaps using something like x * 17 + y * 13 instead of x ^ y will reduce probability of collisions.

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

Sidebar

Related Questions

i am using the following code for form action, // includes require_once('inc/mysql_wrapper.php'); $DB=get_db_connection(); require_once('inc/defines.inc.php');
The following code defines a sequence of names that are mapped to numbers. It
I'm trying to translate the following JS code to ObjC; It basically defines a
The following code works fine #define open { #define close } #include<stdio.h> #define int
I have the following code. // mfc.cpp : Defines the entry point for the
The following code gives me an exception on the XMLFormatTarget line, but if I
The following code defines two classes ( DeckA and DeckB ) that differ only
in java, the following code defines an array of the predefined class ( myCls
I'm using following code to open a gallery inside of my app Intent intent
The following code generates the compiler error below (after the code), but not if

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.