我不确定为什么以下代码段不起作用:
lower_bound
调用返回密钥7,这是预期的。然后std::max_element
和begin()
迭代器之间的lower_bound()
应返回6,因为它应在键4和键7之间搜索,键7的最大值为6。
但由于某些我无法弄清楚的原因,它正在返回pair(15, 12)
。
bool cmp(const std::pair<T, T>& p1, const std::pair<T, T>& p2)
{
return p1.second < p2.second;
}
int main()
{
std::map< T, T, std::greater<T> > store;
std::map< T, T, std::greater<T> >::iterator found_max, lower;
store[ 4 ] = 2;
store[ 7 ] = 6;
store[ 10 ] = 2;
store[ 15 ] = 12;
lower = store.lower_bound( 8 );
printf("%ld %ld\n", lower->first,lower->second);
found_max = std::max_element(store.begin(), lower, cmp);
printf("%ld %ld\n", found_max->first,found_max->second);
return 0;
}
答案 0 :(得分:1)
std::map< T, T, std::greater<T> > store;
store
的密钥按降序顺序排序。因此,[store.begin(), lower)
包含(15, 22)
和(10, 2)
。具有最大值的元素是(15, 22)
。