向量<pair <int,int >>的upper_bound

use*_*743 4 c++ stl

我试图用upper_bound一个vector<pair<int,int>>,像这样的:

vector<pair<int,int>> data;
auto up = upper_bound(data.begin(), data.end(), 0);
Run Code Online (Sandbox Code Playgroud)

VS2012给我以下错误:

error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const int'
Run Code Online (Sandbox Code Playgroud)

为什么要尝试将a const int与a 进行比较pair<int,int>

我尝试编写自己的比较函数,但没有任何改变。如果这样做,编译器会尝试将a转换pair<int,int>为a const int

And*_*owl 5

您正在将一对与数字进行比较,没有预定义的比较运算符。您可能需要将其更改为如下所示:

auto up = upper_bound(data.begin(), data.end(), make_pair(0, 0));
Run Code Online (Sandbox Code Playgroud)

另外,如果您的应用程序中存在用于比较一对和单个数字的特定逻辑,则可以提供自己的比较功能:

bool cmp(int n, pair<int, int> const& p)
{
    // For instance...
    return ((p.first < n) && (p.second < n));
}

int main()
{
    vector<pair<int,int>> data;
    auto up = upper_bound(data.begin(), data.end(), 0, cmp);
}
Run Code Online (Sandbox Code Playgroud)