title: Optimize the codes which finds an element from a vector
body: <p>I have the following code, which tries to find an element from myPairs, which is a std::vector of MyPair. The code works fine, but I feel it could be more elegant. Can anyone please advice, thanks.</p>
  
  <pre><code>std::optional&lt;MyPair&gt;
  find_right_data(const MyPairs &amp;my_pairs, const MyPoint &amp;pos) {
  
      MyPair pair;
      bool found = false;
  
      double min_dist = std::numeric_limits&lt;double&gt;::max();
      for (size_t i = 0; i &lt; my_pairs.size(); i++) {
  
          MyLine myline = my_pairs[i].second;
          double dist = std::numeric_limits&lt;double&gt;::max();
          find_closest_point(myline, pos, &amp;dist);
  
          if (dist &lt; min_dist) {
              min_dist = dist;
              pair = my_pairs[i];
              found = true;
          }
  
      }
  
      if (found) {
          return pair;
      } else {
          return {};
      }
  }
  </code></pre>