如何对一对向量使用upper_bound,按对的递增顺序排列.第二,然后结对,第一
How to use upper_bound with a vector of pairs, arranged in increasing order of pair.second, and then pair.first?
我使用下面的比较器函数对我的配对向量进行排序。
bool sortbysec(const pair<long long,long long> &a,
const pair<long long,long long> &b)
{
if(a.second < b.second)
{
return true;
}
else if(a.second==b.second)
{
if(a.first<b.first)
{
return true;
}
}
return false;
}
现在我想用给定的值对pair.second
做upper_bound
。我怎么写比较函数,这样我就能得到second = second element
和first最小的第一对?
谢谢。
您想要std::lower_bound
,而不是upper_bound
。像这样:
auto iter = std::lower_bound(
your_contaner.begin(),
your_contaner.end(),
lookup_second,
[](const std::pair<long long,long long>& p, long long second) {
return p.second < second;
}
);
if (iter != your_contaner.end() && iter->second == lookup_second) {
// `iter` points to an element with given `second` and smallest `first`.
// Otherwise, there's no element with given `second`, and `iter` points
// to the leftmost larger element, or `end()`.
}
相关文章:
- 为什么不;名字在地图上是按顺序排列的吗
- C++按字母顺序排列的字符串数组
- 如何在 c++ 中按字母顺序排列数组中的字符串?
- 尝试在 c++ 中按字母顺序排列单词时出现分段错误
- OpenGL,尽管按逆时针顺序排列,但一些法线还是反转了
- 有没有办法在不使用任何迭代的情况下将字符的字母顺序排列
- 我可以使用普通的气泡排序来按字母顺序排列列表吗?
- C 按顺序排列一个指针阵列
- 检查用户输入是否按字母顺序排列
- 数组内存地址总是按最小到最大的顺序排列吗
- 如何在C++上按字母顺序排列列表
- STL 优先级队列按递增顺序排列
- 正十进制整数的递归函数位数,按c++的相反顺序排列
- 按字母顺序排列单词
- 如何按大小写顺序排列字符串(而不是按字典顺序排列)
- C++ 自定义映射键/值不按顺序排列
- 按特定顺序排列矢量元素
- 检查文本文件是否按字母顺序排列
- 对的优先级队列以相反的顺序排列
- 如何将按字母顺序排列的BST排序为按频率排序的树