Hash Table implementation in C++ - How to return a key with a particular value
I am trying to solve a programming question using Hash Tables in C++. It is supposed to be fairly simple implementation of hash tables. I am extremely new to Hash Tables and this is my first try at implementation.
The question is that I have been given an array which contains integers. All but one integer repeats itself twice. I have to find an integer that doesn't.
Input: {1,2,1,3,3}
Output: 2
My solution is that I will start putting these keys inside a hash table and if I find a key inside the hash table beforehand, I will delete that key from the hash table.
My code implementation works but I now I wanted to see how I can get back the right value (2 in this case) since even after erasing the key, the keys remain with value 0.
Here is my code:
int main()
{
int num[5] = {1,2,1,3,3};
map <int,int> mymap;
for(int i=0;i<5;i++)
{
if(mymap.find(num[i])!=mymap.end())
{
mymap.erase(num[i]);
}
else
{
mymap[num[i]] = 10; //10 is just a placeholder value.
}
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
}
Output:
0:0
1:0
2:10
3:0
4:0
c++ hash hashtable
add a comment |
I am trying to solve a programming question using Hash Tables in C++. It is supposed to be fairly simple implementation of hash tables. I am extremely new to Hash Tables and this is my first try at implementation.
The question is that I have been given an array which contains integers. All but one integer repeats itself twice. I have to find an integer that doesn't.
Input: {1,2,1,3,3}
Output: 2
My solution is that I will start putting these keys inside a hash table and if I find a key inside the hash table beforehand, I will delete that key from the hash table.
My code implementation works but I now I wanted to see how I can get back the right value (2 in this case) since even after erasing the key, the keys remain with value 0.
Here is my code:
int main()
{
int num[5] = {1,2,1,3,3};
map <int,int> mymap;
for(int i=0;i<5;i++)
{
if(mymap.find(num[i])!=mymap.end())
{
mymap.erase(num[i]);
}
else
{
mymap[num[i]] = 10; //10 is just a placeholder value.
}
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
}
Output:
0:0
1:0
2:10
3:0
4:0
c++ hash hashtable
hash table, but you are using a map, not an unordered_map (the former is a standard map, the latter is a hash map).
– Matthieu Brucher
Nov 20 '18 at 19:29
add a comment |
I am trying to solve a programming question using Hash Tables in C++. It is supposed to be fairly simple implementation of hash tables. I am extremely new to Hash Tables and this is my first try at implementation.
The question is that I have been given an array which contains integers. All but one integer repeats itself twice. I have to find an integer that doesn't.
Input: {1,2,1,3,3}
Output: 2
My solution is that I will start putting these keys inside a hash table and if I find a key inside the hash table beforehand, I will delete that key from the hash table.
My code implementation works but I now I wanted to see how I can get back the right value (2 in this case) since even after erasing the key, the keys remain with value 0.
Here is my code:
int main()
{
int num[5] = {1,2,1,3,3};
map <int,int> mymap;
for(int i=0;i<5;i++)
{
if(mymap.find(num[i])!=mymap.end())
{
mymap.erase(num[i]);
}
else
{
mymap[num[i]] = 10; //10 is just a placeholder value.
}
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
}
Output:
0:0
1:0
2:10
3:0
4:0
c++ hash hashtable
I am trying to solve a programming question using Hash Tables in C++. It is supposed to be fairly simple implementation of hash tables. I am extremely new to Hash Tables and this is my first try at implementation.
The question is that I have been given an array which contains integers. All but one integer repeats itself twice. I have to find an integer that doesn't.
Input: {1,2,1,3,3}
Output: 2
My solution is that I will start putting these keys inside a hash table and if I find a key inside the hash table beforehand, I will delete that key from the hash table.
My code implementation works but I now I wanted to see how I can get back the right value (2 in this case) since even after erasing the key, the keys remain with value 0.
Here is my code:
int main()
{
int num[5] = {1,2,1,3,3};
map <int,int> mymap;
for(int i=0;i<5;i++)
{
if(mymap.find(num[i])!=mymap.end())
{
mymap.erase(num[i]);
}
else
{
mymap[num[i]] = 10; //10 is just a placeholder value.
}
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
}
Output:
0:0
1:0
2:10
3:0
4:0
c++ hash hashtable
c++ hash hashtable
edited Nov 20 '18 at 19:33
Matthieu Brucher
15.9k32141
15.9k32141
asked Nov 20 '18 at 19:23
caspiancaspian
234
234
hash table, but you are using a map, not an unordered_map (the former is a standard map, the latter is a hash map).
– Matthieu Brucher
Nov 20 '18 at 19:29
add a comment |
hash table, but you are using a map, not an unordered_map (the former is a standard map, the latter is a hash map).
– Matthieu Brucher
Nov 20 '18 at 19:29
hash table, but you are using a map, not an unordered_map (the former is a standard map, the latter is a hash map).
– Matthieu Brucher
Nov 20 '18 at 19:29
hash table, but you are using a map, not an unordered_map (the former is a standard map, the latter is a hash map).
– Matthieu Brucher
Nov 20 '18 at 19:29
add a comment |
2 Answers
2
active
oldest
votes
std::map
is a tree, not a hash table. For a hash table you want std::unordered_map
.
But to answer your question:
You can use the map iterator to get the only remaining value:
if (!mymap.empty()) {
cout << mymap.begin()->first;
}
But beware - when you call cout << mymap[X]
, it also adds X
to the map. So remove all those debugging lines at the end.
And by the way - when you don't have a value, just a key, then you can use a set
instead (or unordered_set
).
For some reason, directly implementing your code did not work, but using iterator from the link that you provided me worked. if (!mymap.empty()) { auto it = mymap.begin(); cout << it->first; }
– caspian
Nov 20 '18 at 19:44
Also, thank you for ironing out the basics. Your answer was helpful.
– caspian
Nov 20 '18 at 19:45
Ah yes was thinking about the set, answer updated. Should use->first
instead (but consider usingset
)
– rustyx
Nov 20 '18 at 19:53
add a comment |
Just increment the value in the map, as the integers are default constructed (and thus initialized to 0
):
int num[5] = {1,2,1,3,3};
unordered_map <int,int> mymap;
for(int i=0;i<5;i++)
{
++mymap[num[i]];
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
add a comment |
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%2f53400125%2fhash-table-implementation-in-c-how-to-return-a-key-with-a-particular-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
std::map
is a tree, not a hash table. For a hash table you want std::unordered_map
.
But to answer your question:
You can use the map iterator to get the only remaining value:
if (!mymap.empty()) {
cout << mymap.begin()->first;
}
But beware - when you call cout << mymap[X]
, it also adds X
to the map. So remove all those debugging lines at the end.
And by the way - when you don't have a value, just a key, then you can use a set
instead (or unordered_set
).
For some reason, directly implementing your code did not work, but using iterator from the link that you provided me worked. if (!mymap.empty()) { auto it = mymap.begin(); cout << it->first; }
– caspian
Nov 20 '18 at 19:44
Also, thank you for ironing out the basics. Your answer was helpful.
– caspian
Nov 20 '18 at 19:45
Ah yes was thinking about the set, answer updated. Should use->first
instead (but consider usingset
)
– rustyx
Nov 20 '18 at 19:53
add a comment |
std::map
is a tree, not a hash table. For a hash table you want std::unordered_map
.
But to answer your question:
You can use the map iterator to get the only remaining value:
if (!mymap.empty()) {
cout << mymap.begin()->first;
}
But beware - when you call cout << mymap[X]
, it also adds X
to the map. So remove all those debugging lines at the end.
And by the way - when you don't have a value, just a key, then you can use a set
instead (or unordered_set
).
For some reason, directly implementing your code did not work, but using iterator from the link that you provided me worked. if (!mymap.empty()) { auto it = mymap.begin(); cout << it->first; }
– caspian
Nov 20 '18 at 19:44
Also, thank you for ironing out the basics. Your answer was helpful.
– caspian
Nov 20 '18 at 19:45
Ah yes was thinking about the set, answer updated. Should use->first
instead (but consider usingset
)
– rustyx
Nov 20 '18 at 19:53
add a comment |
std::map
is a tree, not a hash table. For a hash table you want std::unordered_map
.
But to answer your question:
You can use the map iterator to get the only remaining value:
if (!mymap.empty()) {
cout << mymap.begin()->first;
}
But beware - when you call cout << mymap[X]
, it also adds X
to the map. So remove all those debugging lines at the end.
And by the way - when you don't have a value, just a key, then you can use a set
instead (or unordered_set
).
std::map
is a tree, not a hash table. For a hash table you want std::unordered_map
.
But to answer your question:
You can use the map iterator to get the only remaining value:
if (!mymap.empty()) {
cout << mymap.begin()->first;
}
But beware - when you call cout << mymap[X]
, it also adds X
to the map. So remove all those debugging lines at the end.
And by the way - when you don't have a value, just a key, then you can use a set
instead (or unordered_set
).
edited Nov 20 '18 at 19:51
answered Nov 20 '18 at 19:31
rustyxrustyx
31.6k697142
31.6k697142
For some reason, directly implementing your code did not work, but using iterator from the link that you provided me worked. if (!mymap.empty()) { auto it = mymap.begin(); cout << it->first; }
– caspian
Nov 20 '18 at 19:44
Also, thank you for ironing out the basics. Your answer was helpful.
– caspian
Nov 20 '18 at 19:45
Ah yes was thinking about the set, answer updated. Should use->first
instead (but consider usingset
)
– rustyx
Nov 20 '18 at 19:53
add a comment |
For some reason, directly implementing your code did not work, but using iterator from the link that you provided me worked. if (!mymap.empty()) { auto it = mymap.begin(); cout << it->first; }
– caspian
Nov 20 '18 at 19:44
Also, thank you for ironing out the basics. Your answer was helpful.
– caspian
Nov 20 '18 at 19:45
Ah yes was thinking about the set, answer updated. Should use->first
instead (but consider usingset
)
– rustyx
Nov 20 '18 at 19:53
For some reason, directly implementing your code did not work, but using iterator from the link that you provided me worked. if (!mymap.empty()) { auto it = mymap.begin(); cout << it->first; }
– caspian
Nov 20 '18 at 19:44
For some reason, directly implementing your code did not work, but using iterator from the link that you provided me worked. if (!mymap.empty()) { auto it = mymap.begin(); cout << it->first; }
– caspian
Nov 20 '18 at 19:44
Also, thank you for ironing out the basics. Your answer was helpful.
– caspian
Nov 20 '18 at 19:45
Also, thank you for ironing out the basics. Your answer was helpful.
– caspian
Nov 20 '18 at 19:45
Ah yes was thinking about the set, answer updated. Should use
->first
instead (but consider using set
)– rustyx
Nov 20 '18 at 19:53
Ah yes was thinking about the set, answer updated. Should use
->first
instead (but consider using set
)– rustyx
Nov 20 '18 at 19:53
add a comment |
Just increment the value in the map, as the integers are default constructed (and thus initialized to 0
):
int num[5] = {1,2,1,3,3};
unordered_map <int,int> mymap;
for(int i=0;i<5;i++)
{
++mymap[num[i]];
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
add a comment |
Just increment the value in the map, as the integers are default constructed (and thus initialized to 0
):
int num[5] = {1,2,1,3,3};
unordered_map <int,int> mymap;
for(int i=0;i<5;i++)
{
++mymap[num[i]];
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
add a comment |
Just increment the value in the map, as the integers are default constructed (and thus initialized to 0
):
int num[5] = {1,2,1,3,3};
unordered_map <int,int> mymap;
for(int i=0;i<5;i++)
{
++mymap[num[i]];
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
Just increment the value in the map, as the integers are default constructed (and thus initialized to 0
):
int num[5] = {1,2,1,3,3};
unordered_map <int,int> mymap;
for(int i=0;i<5;i++)
{
++mymap[num[i]];
}
cout<<"0:"<<mymap[0]<<endl;
cout<<"1:"<<mymap[1]<<endl;
cout<<"2:"<<mymap[2]<<endl; //Should only find 10 value in 2
cout<<"3:"<<mymap[3]<<endl;
cout<<"4:"<<mymap[4]<<endl;
answered Nov 20 '18 at 19:32
Matthieu BrucherMatthieu Brucher
15.9k32141
15.9k32141
add a comment |
add a comment |
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%2f53400125%2fhash-table-implementation-in-c-how-to-return-a-key-with-a-particular-value%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
hash table, but you are using a map, not an unordered_map (the former is a standard map, the latter is a hash map).
– Matthieu Brucher
Nov 20 '18 at 19:29