我有以下代码,它试图从 myPairs 中找到一个元素,它是 MyPair 的 std::vector。代码工作正常,但我觉得它可以更优雅。有谁能给点建议,谢谢。
std::optional<MyPair>
find_right_data(const MyPairs &my_pairs, const MyPoint &pos) {
MyPair pair;
bool found = false;
double min_dist = std::numeric_limits<double>::max();
for (size_t i = 0; i < my_pairs.size(); i++) {
MyLine myline = my_pairs[i].second;
double dist = std::numeric_limits<double>::max();
find_closest_point(myline, pos, &dist);
if (dist < min_dist) {
min_dist = dist;
pair = my_pairs[i];
found = true;
}
}
if (found) {
return pair;
} else {
return {};
}
}