Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. std::equal_range for structs
QtWS25 Last Chance

std::equal_range for structs

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 4 Posters 563 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on last edited by
    #1

    Hi!

    stuct mystuct{
      int a;
      int b;
      int c;
    };
     
    std::vector<mystruct> mylist;
     
    auto range = std::equal_range(mylist.begin(), mylist.end(), ....);
    

    How to get stucts with b = 2 ?

    KroMignonK 1 Reply Last reply
    0
    • sitesvS sitesv

      Hi!

      stuct mystuct{
        int a;
        int b;
        int c;
      };
       
      std::vector<mystruct> mylist;
       
      auto range = std::equal_range(mylist.begin(), mylist.end(), ....);
      

      How to get stucts with b = 2 ?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @sitesv said in std::equal_range for structs:

      How to get stucts with b = 2 ?

      Why do you not use std::find_if()?:

      stuct mystuct{
        int a;
        int b;
        int c;
      };
       
      std::vector<mystruct> mylist;
      
      auto filter = [](const mystuct& s) { return s.b == 2; };
      auto iter = std::find_if(mylist.begin(), mylist.end(), filter);
      while(iter != mylist.end())
      {
         // do stuff with *iter
      
         // search next
         iter = std::find_if(std::next(iter), mylist.end(), filter);
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      sitesvS 1 Reply Last reply
      4
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Don't see any relation to Qt here. The cppreference documentation has an example which matches exactly your problem.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        SGaistS 1 Reply Last reply
        2
        • KroMignonK KroMignon

          @sitesv said in std::equal_range for structs:

          How to get stucts with b = 2 ?

          Why do you not use std::find_if()?:

          stuct mystuct{
            int a;
            int b;
            int c;
          };
           
          std::vector<mystruct> mylist;
          
          auto filter = [](const mystuct& s) { return s.b == 2; };
          auto iter = std::find_if(mylist.begin(), mylist.end(), filter);
          while(iter != mylist.end())
          {
             // do stuff with *iter
          
             // search next
             iter = std::find_if(std::next(iter), mylist.end(), filter);
          }
          
          sitesvS Offline
          sitesvS Offline
          sitesv
          wrote on last edited by sitesv
          #4

          Is it possible to use equal_range with vector of pointers?
          I need to edit vector elements in runtime... Example from doc is not working in this case cause of 'const'....

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Don't see any relation to Qt here. The cppreference documentation has an example which matches exactly your problem.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @sitesv said in std::equal_range for structs:

            Is it possible to use equal_range with vector of pointers?

            Yes it is, but you will have to provide the comparison function yourself.

            It's all shown in the documentation that @Christian-Ehrlicher linked to.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved