如何在C 对向量的第二个元素上执行较低的操作

How to perform a lower_bound operation on the second elements of a vector of pairs in C++?

本文关键字:执行 操作 元素 第二个 向量      更新时间:2023-10-16

一个人可以将所有 v[i].second元素复制到新向量,然后在新向量上进行lower_bound操作,并且作为我们所搜索的特定值的位置,在这两个方面都是相同的向量,因此可以做到合理。但是,如果矢量有很多元素,那么复制将是昂贵的。

因此,如果可能的话,我想知道在vector<pair<int,int>>类型的容器上执行lower_bound操作的语法(特别是在向量的第二个元素上(。

int myValue = 42;  // value to search for.
std::lower_bound(myVector.begin(), myVector.end(), myValue,
  [](const pair<int,int>& a, int b) {
    return a.second < b;
  });

这假设向量由element.second分类。