0
votes

When I run "upper_bound" with a comparator it gives me this error: error: reference to type 'const std::vector<int, std::allocator>' could not bind to an lvalue of type 'const int. Anyone know the issue?

static bool compareInterval(const vector<int>& a, const vector<int>& b) {
    return (a[0] < b[0]);
}

vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
    vector<int> ret(n, 0);
    sort(bookings.begin(), bookings.end(), compareInterval);
    
    for (int i = 0; i < n; ++i) {
        auto low=upper_bound(bookings.begin(), bookings.end(), i, compareInterval);
    }
    return ret;
}

In file included from prog_joined.cpp:1: In file included from ./precompiled/headers.h:13: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/cmath:1927: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/specfun.h:45: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algobase.h:71: /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/predefined_ops.h:215:24: error: reference to type 'const std::vector<int, std::allocator>' could not bind to an lvalue of type 'const int' { return bool(_M_comp(__val, __it)); } ^~~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:2051:8: note: in instantiation of function template specialization '__gnu_cxx::__ops::_Val_comp_iter<bool ()(const std::vector<int, std::allocator> &, const std::vector<int, std::allocator> &)>::operator()<const int, __gnu_cxx::__normal_iterator<std::vector<int, std::allocator> *, std::vector<std::vector<int, std::allocator>, std::allocator<std::vector<int, std::allocator>>>>>' requested here if (__comp(__val, __middle)) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:2116:19: note: in instantiation of function template specialization 'std::__upper_bound<__gnu_cxx::__normal_iterator<std::vector<int, std::allocator> , std::vector<std::vector<int, std::allocator>, std::allocator<std::vector<int, std::allocator>>>>, int, __gnu_cxx::__ops::_Val_comp_iter<bool ()(const std::vector<int, std::allocator> &, const std::vector<int, std::allocator> &)>>' requested here return std::__upper_bound(__first, __last, __val, ^ Line 12: Char 22: note: in instantiation of function template specialization 'std::upper_bound<__gnu_cxx::__normal_iterator<std::vector<int, std::allocator> , std::vector<std::vector<int, std::allocator>, std::allocator<std::vector<int, std::allocator>>>>, int, bool ()(const std::vector<int, std::allocator> &, const std::vector<int, std::allocator> &)>' requested here auto low=upper_bound(bookings.begin(), bookings.end(), i, compareInterval); ^ 1 error generated.

1

1 Answers

0
votes

It turns out "i" needs to be wrapped up to become the same type as an element of bookings