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. finding the closest or equal numer in a vector.
Forum Updated to NodeBB v4.3 + New Features

finding the closest or equal numer in a vector.

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 992 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by Natural_Bugger
    #1

    hello,

    i'm looking for a value that is closest but smaller or equal to b.
    the vector contains random numbers smaller or bigger than b.

    this method doesn't cut the mostard, somehow

        vector<int> temp;
        int tempNum = 0;
        for(auto ii : temp){
            if(ii <= b){
                //printf("Integer value is %d\n" , ii);
                tempNum = ii;
            }
        }
    
    JonBJ J.HilkJ 2 Replies Last reply
    0
    • N Natural_Bugger

      hello,

      i'm looking for a value that is closest but smaller or equal to b.
      the vector contains random numbers smaller or bigger than b.

      this method doesn't cut the mostard, somehow

          vector<int> temp;
          int tempNum = 0;
          for(auto ii : temp){
              if(ii <= b){
                  //printf("Integer value is %d\n" , ii);
                  tempNum = ii;
              }
          }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Natural_Bugger
      Well, it doesn't "cut the mostard, somehow" because you seem to check whether something is smaller each time but not whether it's closer than the current best guess.

      It doesn't help that your code uses unhelpful variable names and doesn't declare/initialize them all!

      You will want something like:

      int target = <whatever the target is>;
      int closestLessThanOrEqualTo;
      bool foundAnyClosest = false;
      for (auto num : vector)
          if (num <= target)
              if (!foundAnyClosest)
              {
                  closestLessThanOrEqualTo = num;
                  foundAnyClosest = true;
              }
              else if (num > closestLessThanOrEqualTo)
                  closestLessThanOrEqualTo = num;
      
      N 1 Reply Last reply
      0
      • N Natural_Bugger

        hello,

        i'm looking for a value that is closest but smaller or equal to b.
        the vector contains random numbers smaller or bigger than b.

        this method doesn't cut the mostard, somehow

            vector<int> temp;
            int tempNum = 0;
            for(auto ii : temp){
                if(ii <= b){
                    //printf("Integer value is %d\n" , ii);
                    tempNum = ii;
                }
            }
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @Natural_Bugger
        there's
        std::lower_bound and std::upper_bound

        but in your case, of an unordered container those may not return the absolute closest match

        actually
        std::nth_element

        could be what you're looking for :D


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • JonBJ JonB

          @Natural_Bugger
          Well, it doesn't "cut the mostard, somehow" because you seem to check whether something is smaller each time but not whether it's closer than the current best guess.

          It doesn't help that your code uses unhelpful variable names and doesn't declare/initialize them all!

          You will want something like:

          int target = <whatever the target is>;
          int closestLessThanOrEqualTo;
          bool foundAnyClosest = false;
          for (auto num : vector)
              if (num <= target)
                  if (!foundAnyClosest)
                  {
                      closestLessThanOrEqualTo = num;
                      foundAnyClosest = true;
                  }
                  else if (num > closestLessThanOrEqualTo)
                      closestLessThanOrEqualTo = num;
          
          N Offline
          N Offline
          Natural_Bugger
          wrote on last edited by
          #4

          @JonB

          thnx for your help.
          when i sorted the vector, it worked, mine aswel yours.

          how hard can it be
          : )

          1 Reply Last reply
          0

          • Login

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