Przypadek gdy nie znaleziono wartości w podanym zakresie - lower_bound zwróci ostatni iterator:
if (it != wysokosci.begin() + n + 1 and *it == p) {
Kod inaczej:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int count;
std::cin >> count;
std::vector<int> boxes(count);
for (auto& box : boxes) {
std::cin >> box;
}
int queries;
std::cin >> queries;
while (queries--) {
int height;
std::cin >> height;
auto it = std::lower_bound(boxes.begin(), boxes.end(), height);
if (it != boxes.end() and *it == height) {
std::cout << it - boxes.begin() + 1 << "\n";
} else {
std::cout << "Kup pudelko!\n";
}
}
}