How to determine whether a value exists in map in C++












-7















I understand that std::map is (Key, Value) pairs.



I want to search through the values of a map. Let us say that I want to find the highest value among the values in the std::map. How can I do that ?
For example let me consider a map like this:




John -> 100



Jeffrey -> 200



Krishna -> 147




I think it will be similar to this , but I am not sure.



 for (auto it=m.begin(); it!=m.end(); it++)
{
if (it->second == 500)
{
cout << "Found";
}
else {
continue;}
}


Instead of iterating through std::map, is there any other inbuilt method using which I can check if a value exists in a std::map with O(1) time complexity ?










share|improve this question

























  • well, yes :) that's it

    – OznOg
    Nov 23 '18 at 17:07






  • 2





    en.cppreference.com/w/cpp/container/map

    – Lightness Races in Orbit
    Nov 23 '18 at 17:08











  • are you actually searching for the max or a random value?

    – OznOg
    Nov 23 '18 at 17:08






  • 1





    en.cppreference.com/w/cpp/algorithm

    – Lightness Races in Orbit
    Nov 23 '18 at 17:09






  • 1





    First you say map then you say hashmap - they are different make your mind

    – Slava
    Nov 23 '18 at 17:09


















-7















I understand that std::map is (Key, Value) pairs.



I want to search through the values of a map. Let us say that I want to find the highest value among the values in the std::map. How can I do that ?
For example let me consider a map like this:




John -> 100



Jeffrey -> 200



Krishna -> 147




I think it will be similar to this , but I am not sure.



 for (auto it=m.begin(); it!=m.end(); it++)
{
if (it->second == 500)
{
cout << "Found";
}
else {
continue;}
}


Instead of iterating through std::map, is there any other inbuilt method using which I can check if a value exists in a std::map with O(1) time complexity ?










share|improve this question

























  • well, yes :) that's it

    – OznOg
    Nov 23 '18 at 17:07






  • 2





    en.cppreference.com/w/cpp/container/map

    – Lightness Races in Orbit
    Nov 23 '18 at 17:08











  • are you actually searching for the max or a random value?

    – OznOg
    Nov 23 '18 at 17:08






  • 1





    en.cppreference.com/w/cpp/algorithm

    – Lightness Races in Orbit
    Nov 23 '18 at 17:09






  • 1





    First you say map then you say hashmap - they are different make your mind

    – Slava
    Nov 23 '18 at 17:09
















-7












-7








-7


1






I understand that std::map is (Key, Value) pairs.



I want to search through the values of a map. Let us say that I want to find the highest value among the values in the std::map. How can I do that ?
For example let me consider a map like this:




John -> 100



Jeffrey -> 200



Krishna -> 147




I think it will be similar to this , but I am not sure.



 for (auto it=m.begin(); it!=m.end(); it++)
{
if (it->second == 500)
{
cout << "Found";
}
else {
continue;}
}


Instead of iterating through std::map, is there any other inbuilt method using which I can check if a value exists in a std::map with O(1) time complexity ?










share|improve this question
















I understand that std::map is (Key, Value) pairs.



I want to search through the values of a map. Let us say that I want to find the highest value among the values in the std::map. How can I do that ?
For example let me consider a map like this:




John -> 100



Jeffrey -> 200



Krishna -> 147




I think it will be similar to this , but I am not sure.



 for (auto it=m.begin(); it!=m.end(); it++)
{
if (it->second == 500)
{
cout << "Found";
}
else {
continue;}
}


Instead of iterating through std::map, is there any other inbuilt method using which I can check if a value exists in a std::map with O(1) time complexity ?







c++ stl hashtable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 29 at 14:02







Nitz

















asked Nov 23 '18 at 17:06









NitzNitz

64




64













  • well, yes :) that's it

    – OznOg
    Nov 23 '18 at 17:07






  • 2





    en.cppreference.com/w/cpp/container/map

    – Lightness Races in Orbit
    Nov 23 '18 at 17:08











  • are you actually searching for the max or a random value?

    – OznOg
    Nov 23 '18 at 17:08






  • 1





    en.cppreference.com/w/cpp/algorithm

    – Lightness Races in Orbit
    Nov 23 '18 at 17:09






  • 1





    First you say map then you say hashmap - they are different make your mind

    – Slava
    Nov 23 '18 at 17:09





















  • well, yes :) that's it

    – OznOg
    Nov 23 '18 at 17:07






  • 2





    en.cppreference.com/w/cpp/container/map

    – Lightness Races in Orbit
    Nov 23 '18 at 17:08











  • are you actually searching for the max or a random value?

    – OznOg
    Nov 23 '18 at 17:08






  • 1





    en.cppreference.com/w/cpp/algorithm

    – Lightness Races in Orbit
    Nov 23 '18 at 17:09






  • 1





    First you say map then you say hashmap - they are different make your mind

    – Slava
    Nov 23 '18 at 17:09



















well, yes :) that's it

– OznOg
Nov 23 '18 at 17:07





well, yes :) that's it

– OznOg
Nov 23 '18 at 17:07




2




2





en.cppreference.com/w/cpp/container/map

– Lightness Races in Orbit
Nov 23 '18 at 17:08





en.cppreference.com/w/cpp/container/map

– Lightness Races in Orbit
Nov 23 '18 at 17:08













are you actually searching for the max or a random value?

– OznOg
Nov 23 '18 at 17:08





are you actually searching for the max or a random value?

– OznOg
Nov 23 '18 at 17:08




1




1





en.cppreference.com/w/cpp/algorithm

– Lightness Races in Orbit
Nov 23 '18 at 17:09





en.cppreference.com/w/cpp/algorithm

– Lightness Races in Orbit
Nov 23 '18 at 17:09




1




1





First you say map then you say hashmap - they are different make your mind

– Slava
Nov 23 '18 at 17:09







First you say map then you say hashmap - they are different make your mind

– Slava
Nov 23 '18 at 17:09














2 Answers
2






active

oldest

votes


















1















Q1: How to check if a value exists in hashmap?




You need to iterate through and check if such item exists. You can use `std::find_if() with a lambda or do that through a loop. If you do that quite often you may want to index values as well (see below)




Q2: How to iterate through all the values in a map and find the largest value or the smallest value ?




Again you iterate through container and find it or you can use std::max_element() or std::min_element() with a lambda as well. Though if you need to access values in sorted order you may consider to use boost::multimap which will allow to access data using hashed index by name and provide sorted or hashed index(es) for values, though you should be aware of a price of every index added.






share|improve this answer































    0














    For the second question, use:



    std::map<std::string, int> foo = {{"John",100},{"Jeffrey",200},{"Krishna",147}};
    std::cout << std::max_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;
    std::cout << std::min_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;


    Use an adapted lambda with std::find_if, you should be able to find also if a value exists in a map (or hash table).






    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%2f53450604%2fhow-to-determine-whether-a-value-exists-in-map-in-c%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















      Q1: How to check if a value exists in hashmap?




      You need to iterate through and check if such item exists. You can use `std::find_if() with a lambda or do that through a loop. If you do that quite often you may want to index values as well (see below)




      Q2: How to iterate through all the values in a map and find the largest value or the smallest value ?




      Again you iterate through container and find it or you can use std::max_element() or std::min_element() with a lambda as well. Though if you need to access values in sorted order you may consider to use boost::multimap which will allow to access data using hashed index by name and provide sorted or hashed index(es) for values, though you should be aware of a price of every index added.






      share|improve this answer




























        1















        Q1: How to check if a value exists in hashmap?




        You need to iterate through and check if such item exists. You can use `std::find_if() with a lambda or do that through a loop. If you do that quite often you may want to index values as well (see below)




        Q2: How to iterate through all the values in a map and find the largest value or the smallest value ?




        Again you iterate through container and find it or you can use std::max_element() or std::min_element() with a lambda as well. Though if you need to access values in sorted order you may consider to use boost::multimap which will allow to access data using hashed index by name and provide sorted or hashed index(es) for values, though you should be aware of a price of every index added.






        share|improve this answer


























          1












          1








          1








          Q1: How to check if a value exists in hashmap?




          You need to iterate through and check if such item exists. You can use `std::find_if() with a lambda or do that through a loop. If you do that quite often you may want to index values as well (see below)




          Q2: How to iterate through all the values in a map and find the largest value or the smallest value ?




          Again you iterate through container and find it or you can use std::max_element() or std::min_element() with a lambda as well. Though if you need to access values in sorted order you may consider to use boost::multimap which will allow to access data using hashed index by name and provide sorted or hashed index(es) for values, though you should be aware of a price of every index added.






          share|improve this answer














          Q1: How to check if a value exists in hashmap?




          You need to iterate through and check if such item exists. You can use `std::find_if() with a lambda or do that through a loop. If you do that quite often you may want to index values as well (see below)




          Q2: How to iterate through all the values in a map and find the largest value or the smallest value ?




          Again you iterate through container and find it or you can use std::max_element() or std::min_element() with a lambda as well. Though if you need to access values in sorted order you may consider to use boost::multimap which will allow to access data using hashed index by name and provide sorted or hashed index(es) for values, though you should be aware of a price of every index added.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 17:18









          SlavaSlava

          32.6k12865




          32.6k12865

























              0














              For the second question, use:



              std::map<std::string, int> foo = {{"John",100},{"Jeffrey",200},{"Krishna",147}};
              std::cout << std::max_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;
              std::cout << std::min_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;


              Use an adapted lambda with std::find_if, you should be able to find also if a value exists in a map (or hash table).






              share|improve this answer






























                0














                For the second question, use:



                std::map<std::string, int> foo = {{"John",100},{"Jeffrey",200},{"Krishna",147}};
                std::cout << std::max_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;
                std::cout << std::min_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;


                Use an adapted lambda with std::find_if, you should be able to find also if a value exists in a map (or hash table).






                share|improve this answer




























                  0












                  0








                  0







                  For the second question, use:



                  std::map<std::string, int> foo = {{"John",100},{"Jeffrey",200},{"Krishna",147}};
                  std::cout << std::max_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;
                  std::cout << std::min_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;


                  Use an adapted lambda with std::find_if, you should be able to find also if a value exists in a map (or hash table).






                  share|improve this answer















                  For the second question, use:



                  std::map<std::string, int> foo = {{"John",100},{"Jeffrey",200},{"Krishna",147}};
                  std::cout << std::max_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;
                  std::cout << std::min_element(foo.begin(), foo.end(), (const auto& p1, const auto& p2){return p1.second < p2.second;})->first;


                  Use an adapted lambda with std::find_if, you should be able to find also if a value exists in a map (or hash table).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 17:21

























                  answered Nov 23 '18 at 17:13









                  Matthieu BrucherMatthieu Brucher

                  15.9k32141




                  15.9k32141






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450604%2fhow-to-determine-whether-a-value-exists-in-map-in-c%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

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin