Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved std::equal_range for structs

    C++ Gurus
    4
    5
    181
    Loading More Posts
    • 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.
    • sitesv
      sitesv last edited by

      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 ?

      KroMignon 1 Reply Last reply Reply Quote 0
      • KroMignon
        KroMignon @sitesv last edited by

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

        sitesv 1 Reply Last reply Reply Quote 4
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

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

          Qt has to stay free or it will die.

          SGaist 1 Reply Last reply Reply Quote 2
          • sitesv
            sitesv @KroMignon last edited by sitesv

            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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion @Christian Ehrlicher last edited by

              @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 Reply Quote 1
              • First post
                Last post