Hash Table implementation in C++ - How to return a key with a particular value












0














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









share|improve this question
























  • 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 at 19:29


















0














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









share|improve this question
























  • 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 at 19:29
















0












0








0







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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 19:33









Matthieu Brucher

11.5k22137




11.5k22137










asked Nov 20 at 19:23









caspian

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 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 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 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 at 19:29














2 Answers
2






active

oldest

votes


















1














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).






share|improve this answer























  • 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 at 19:44










  • Also, thank you for ironing out the basics. Your answer was helpful.
    – caspian
    Nov 20 at 19:45












  • Ah yes was thinking about the set, answer updated. Should use ->first instead (but consider using set)
    – rustyx
    Nov 20 at 19:53





















1














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;





share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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).






    share|improve this answer























    • 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 at 19:44










    • Also, thank you for ironing out the basics. Your answer was helpful.
      – caspian
      Nov 20 at 19:45












    • Ah yes was thinking about the set, answer updated. Should use ->first instead (but consider using set)
      – rustyx
      Nov 20 at 19:53


















    1














    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).






    share|improve this answer























    • 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 at 19:44










    • Also, thank you for ironing out the basics. Your answer was helpful.
      – caspian
      Nov 20 at 19:45












    • Ah yes was thinking about the set, answer updated. Should use ->first instead (but consider using set)
      – rustyx
      Nov 20 at 19:53
















    1












    1








    1






    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).






    share|improve this answer














    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).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 19:51

























    answered Nov 20 at 19:31









    rustyx

    28.4k695136




    28.4k695136












    • 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 at 19:44










    • Also, thank you for ironing out the basics. Your answer was helpful.
      – caspian
      Nov 20 at 19:45












    • Ah yes was thinking about the set, answer updated. Should use ->first instead (but consider using set)
      – rustyx
      Nov 20 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 at 19:44










    • Also, thank you for ironing out the basics. Your answer was helpful.
      – caspian
      Nov 20 at 19:45












    • Ah yes was thinking about the set, answer updated. Should use ->first instead (but consider using set)
      – rustyx
      Nov 20 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 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 at 19:44












    Also, thank you for ironing out the basics. Your answer was helpful.
    – caspian
    Nov 20 at 19:45






    Also, thank you for ironing out the basics. Your answer was helpful.
    – caspian
    Nov 20 at 19:45














    Ah yes was thinking about the set, answer updated. Should use ->first instead (but consider using set)
    – rustyx
    Nov 20 at 19:53






    Ah yes was thinking about the set, answer updated. Should use ->first instead (but consider using set)
    – rustyx
    Nov 20 at 19:53















    1














    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;





    share|improve this answer


























      1














      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;





      share|improve this answer
























        1












        1








        1






        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;





        share|improve this answer












        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;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 19:32









        Matthieu Brucher

        11.5k22137




        11.5k22137






























            draft saved

            draft discarded




















































            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'