Create an unordered_map with custom object as keyHash Object problem in C++Is there any advantage of using map over unordered_map in case of trivial keys?Using an object reference as a key in std::unordered_mapUsing std::pair as key in std::unordered_mapC++ unordered_map using a custom class type as the keyWhen should we provide our own Hash function for `std::unordered_set`Why should I use a pointer rather than the object itself?Unordered Map with three unsigned chars as keyReturn type mismatch for operator implementation in unordered_map wrapperC++ - type qualifiers error when hashing user defined object
Python 3 - simple temperature program version 1.3
What is monoid homomorphism exactly?
Where to draw the line between quantum mechanics theory and its interpretation(s)?
Can you figure out this language?
How can I finally understand the confusing modal verb "мочь"?
Why increasing of the temperature of the objects like wood, paper etc. doesn't fire them?
Huffman Code in C++
What word describes the sound of an instrument based on the shape of the waveform of its sound?
All of my Firefox add-ons been disabled suddenly, how can I re-enable them?
Copper as an adjective to refer to something made of copper
How is trade in services conducted under the WTO in the absence of the Doha conclusion?
What is the meaning of 「隣のおじいさんは言いました」
Changing stroke width vertically but not horizontally in Inkscape
Can anyone identify this unknown 1988 PC card from The Palantir Corporation?
Problem with estimating a sequence with intuition
Why can't argument be forwarded inside lambda without mutable?
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
What detail can Hubble see on Mars?
What does the phrase "go for the pin" mean here?
Where did Lovecraft write about Carcosa?
What are the requirements for a river delta to form?
Gerrymandering Puzzle - Rig the Election
Is crescere the correct word meaning to to grow or cultivate?
How to preserve a rare version of a book?
Create an unordered_map with custom object as key
Hash Object problem in C++Is there any advantage of using map over unordered_map in case of trivial keys?Using an object reference as a key in std::unordered_mapUsing std::pair as key in std::unordered_mapC++ unordered_map using a custom class type as the keyWhen should we provide our own Hash function for `std::unordered_set`Why should I use a pointer rather than the object itself?Unordered Map with three unsigned chars as keyReturn type mismatch for operator implementation in unordered_map wrapperC++ - type qualifiers error when hashing user defined object
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to create an std::unordered_map
with Node
as the key. I have already overloaded the operator ==
and <
. I have also defined a hash function. However, it is giving me error:
C2280: std::hash<_Kty>::hash(const std::hash<_Kty> &): attempting to reference a deleted function
Here is my code for the Node
class:
#include "Node.h"
Node::Node()
word = Word();
Node::Node(Word input_word) : word(input_word)
Node::Node(const Node& other)
word = other.word;
connection = other.connection;
Node::~Node()
bool Node::operator<(const Node& other)
return (word < other.get_word());
bool Node::operator==(const Node& other)
if (word == other.word)
if (connection.size() == other.connection.size())
for (unsigned int i = 0; i < connection.size(); i++)
if (connection[i] != other.connection[i])
return false;
return true;
else
return false;
else
return false;
Word Node::get_word() const
return word;
int Node::add_connection(Edge* input_node)
connection.push_back(input_node);
return SUCCESS;
vector<Edge*> Node::retrieve_connection() const
return connection;
namespace std
template <> struct hash<Node>
size_t operator()(const Node& other) const
return hash<string>()(other.get_word().get_string()) ^
hash<int>()(other.retrieve_connection().size());
;
I am very confused about what I should add next. I have tried most suggestions that I can find on websites, but none of them works.
Thank you for your attention!
c++ hash unordered-map
add a comment |
I want to create an std::unordered_map
with Node
as the key. I have already overloaded the operator ==
and <
. I have also defined a hash function. However, it is giving me error:
C2280: std::hash<_Kty>::hash(const std::hash<_Kty> &): attempting to reference a deleted function
Here is my code for the Node
class:
#include "Node.h"
Node::Node()
word = Word();
Node::Node(Word input_word) : word(input_word)
Node::Node(const Node& other)
word = other.word;
connection = other.connection;
Node::~Node()
bool Node::operator<(const Node& other)
return (word < other.get_word());
bool Node::operator==(const Node& other)
if (word == other.word)
if (connection.size() == other.connection.size())
for (unsigned int i = 0; i < connection.size(); i++)
if (connection[i] != other.connection[i])
return false;
return true;
else
return false;
else
return false;
Word Node::get_word() const
return word;
int Node::add_connection(Edge* input_node)
connection.push_back(input_node);
return SUCCESS;
vector<Edge*> Node::retrieve_connection() const
return connection;
namespace std
template <> struct hash<Node>
size_t operator()(const Node& other) const
return hash<string>()(other.get_word().get_string()) ^
hash<int>()(other.retrieve_connection().size());
;
I am very confused about what I should add next. I have tried most suggestions that I can find on websites, but none of them works.
Thank you for your attention!
c++ hash unordered-map
3
Please post a Minimal, Complete, and Verifiable example and the complete error messages from the compiler.
– R Sahu
Mar 23 at 4:39
1
"I have tried most suggestions that I can find on websites, but none of them works." It's hard for us to help you when we can't try suggestions, get error messages with our preferred compiler, simplify the code to see if the error goes away, and so on.
– David Schwartz
Mar 23 at 5:20
I had to make a lot of assumptions about yourNode
andWord
classes (many of them bad coding style; e.g. you should use= default
for constructors and destructors when possible, or in this case just use the Rule of Zero and don't explicitly define them at all), but when I did everything compiled perfectly. Please share the full error message and see if you can provide a complete (non-)working minimal example.
– Daniel H
Mar 23 at 5:21
add a comment |
I want to create an std::unordered_map
with Node
as the key. I have already overloaded the operator ==
and <
. I have also defined a hash function. However, it is giving me error:
C2280: std::hash<_Kty>::hash(const std::hash<_Kty> &): attempting to reference a deleted function
Here is my code for the Node
class:
#include "Node.h"
Node::Node()
word = Word();
Node::Node(Word input_word) : word(input_word)
Node::Node(const Node& other)
word = other.word;
connection = other.connection;
Node::~Node()
bool Node::operator<(const Node& other)
return (word < other.get_word());
bool Node::operator==(const Node& other)
if (word == other.word)
if (connection.size() == other.connection.size())
for (unsigned int i = 0; i < connection.size(); i++)
if (connection[i] != other.connection[i])
return false;
return true;
else
return false;
else
return false;
Word Node::get_word() const
return word;
int Node::add_connection(Edge* input_node)
connection.push_back(input_node);
return SUCCESS;
vector<Edge*> Node::retrieve_connection() const
return connection;
namespace std
template <> struct hash<Node>
size_t operator()(const Node& other) const
return hash<string>()(other.get_word().get_string()) ^
hash<int>()(other.retrieve_connection().size());
;
I am very confused about what I should add next. I have tried most suggestions that I can find on websites, but none of them works.
Thank you for your attention!
c++ hash unordered-map
I want to create an std::unordered_map
with Node
as the key. I have already overloaded the operator ==
and <
. I have also defined a hash function. However, it is giving me error:
C2280: std::hash<_Kty>::hash(const std::hash<_Kty> &): attempting to reference a deleted function
Here is my code for the Node
class:
#include "Node.h"
Node::Node()
word = Word();
Node::Node(Word input_word) : word(input_word)
Node::Node(const Node& other)
word = other.word;
connection = other.connection;
Node::~Node()
bool Node::operator<(const Node& other)
return (word < other.get_word());
bool Node::operator==(const Node& other)
if (word == other.word)
if (connection.size() == other.connection.size())
for (unsigned int i = 0; i < connection.size(); i++)
if (connection[i] != other.connection[i])
return false;
return true;
else
return false;
else
return false;
Word Node::get_word() const
return word;
int Node::add_connection(Edge* input_node)
connection.push_back(input_node);
return SUCCESS;
vector<Edge*> Node::retrieve_connection() const
return connection;
namespace std
template <> struct hash<Node>
size_t operator()(const Node& other) const
return hash<string>()(other.get_word().get_string()) ^
hash<int>()(other.retrieve_connection().size());
;
I am very confused about what I should add next. I have tried most suggestions that I can find on websites, but none of them works.
Thank you for your attention!
c++ hash unordered-map
c++ hash unordered-map
edited Mar 23 at 5:14
melpomene
63.7k55195
63.7k55195
asked Mar 23 at 4:21
Runsheng MaRunsheng Ma
13
13
3
Please post a Minimal, Complete, and Verifiable example and the complete error messages from the compiler.
– R Sahu
Mar 23 at 4:39
1
"I have tried most suggestions that I can find on websites, but none of them works." It's hard for us to help you when we can't try suggestions, get error messages with our preferred compiler, simplify the code to see if the error goes away, and so on.
– David Schwartz
Mar 23 at 5:20
I had to make a lot of assumptions about yourNode
andWord
classes (many of them bad coding style; e.g. you should use= default
for constructors and destructors when possible, or in this case just use the Rule of Zero and don't explicitly define them at all), but when I did everything compiled perfectly. Please share the full error message and see if you can provide a complete (non-)working minimal example.
– Daniel H
Mar 23 at 5:21
add a comment |
3
Please post a Minimal, Complete, and Verifiable example and the complete error messages from the compiler.
– R Sahu
Mar 23 at 4:39
1
"I have tried most suggestions that I can find on websites, but none of them works." It's hard for us to help you when we can't try suggestions, get error messages with our preferred compiler, simplify the code to see if the error goes away, and so on.
– David Schwartz
Mar 23 at 5:20
I had to make a lot of assumptions about yourNode
andWord
classes (many of them bad coding style; e.g. you should use= default
for constructors and destructors when possible, or in this case just use the Rule of Zero and don't explicitly define them at all), but when I did everything compiled perfectly. Please share the full error message and see if you can provide a complete (non-)working minimal example.
– Daniel H
Mar 23 at 5:21
3
3
Please post a Minimal, Complete, and Verifiable example and the complete error messages from the compiler.
– R Sahu
Mar 23 at 4:39
Please post a Minimal, Complete, and Verifiable example and the complete error messages from the compiler.
– R Sahu
Mar 23 at 4:39
1
1
"I have tried most suggestions that I can find on websites, but none of them works." It's hard for us to help you when we can't try suggestions, get error messages with our preferred compiler, simplify the code to see if the error goes away, and so on.
– David Schwartz
Mar 23 at 5:20
"I have tried most suggestions that I can find on websites, but none of them works." It's hard for us to help you when we can't try suggestions, get error messages with our preferred compiler, simplify the code to see if the error goes away, and so on.
– David Schwartz
Mar 23 at 5:20
I had to make a lot of assumptions about your
Node
and Word
classes (many of them bad coding style; e.g. you should use = default
for constructors and destructors when possible, or in this case just use the Rule of Zero and don't explicitly define them at all), but when I did everything compiled perfectly. Please share the full error message and see if you can provide a complete (non-)working minimal example.– Daniel H
Mar 23 at 5:21
I had to make a lot of assumptions about your
Node
and Word
classes (many of them bad coding style; e.g. you should use = default
for constructors and destructors when possible, or in this case just use the Rule of Zero and don't explicitly define them at all), but when I did everything compiled perfectly. Please share the full error message and see if you can provide a complete (non-)working minimal example.– Daniel H
Mar 23 at 5:21
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310590%2fcreate-an-unordered-map-with-custom-object-as-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310590%2fcreate-an-unordered-map-with-custom-object-as-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Please post a Minimal, Complete, and Verifiable example and the complete error messages from the compiler.
– R Sahu
Mar 23 at 4:39
1
"I have tried most suggestions that I can find on websites, but none of them works." It's hard for us to help you when we can't try suggestions, get error messages with our preferred compiler, simplify the code to see if the error goes away, and so on.
– David Schwartz
Mar 23 at 5:20
I had to make a lot of assumptions about your
Node
andWord
classes (many of them bad coding style; e.g. you should use= default
for constructors and destructors when possible, or in this case just use the Rule of Zero and don't explicitly define them at all), but when I did everything compiled perfectly. Please share the full error message and see if you can provide a complete (non-)working minimal example.– Daniel H
Mar 23 at 5:21