简体   繁体   中英

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

One can copy all the v[i].second elements to a new vector and then do the lower_bound operation on the new vector and as the position of the particular value we are searching for would be the same in both vectors so it is reasonable to do. But if the vector has many elements then copying would be costly.

So, I want to know the syntax to perform a lower_bound operation on a vector<pair<int,int>> type of container if possible (specially only on the second elements of the vector).

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;
  });

This assumes the vector is sorted by element.second .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM