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
Forum Update on Monday, May 27th 2025

std::equal_range for structs

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 4 Posters 571 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.
  • S Offline
    S Offline
    sitesv
    wrote on 28 Jul 2021, 13:50 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 ?

    K 1 Reply Last reply 28 Jul 2021, 14:12
    0
    • S sitesv
      28 Jul 2021, 13:50

      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 ?

      K Offline
      K Offline
      KroMignon
      wrote on 28 Jul 2021, 14:12 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)

      S 1 Reply Last reply 28 Jul 2021, 17:59
      4
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 28 Jul 2021, 14:12 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

        S 1 Reply Last reply 28 Jul 2021, 18:04
        2
        • K KroMignon
          28 Jul 2021, 14:12

          @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);
          }
          
          S Offline
          S Offline
          sitesv
          wrote on 28 Jul 2021, 17:59 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
          • C Christian Ehrlicher
            28 Jul 2021, 14:12

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

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 28 Jul 2021, 18:04 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

            1/5

            28 Jul 2021, 13:50

            • Login

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