读书人

STL中 lower_bound与 greater一起使用

发布时间: 2012-02-15 12:09:43 作者: rapoo

STL中 lower_bound与 greater一起使用的一个问题
有下面一段代码:
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;

const int VECTOR_SIZE = 8 ;
typedef vector<int > IntVector ;
typedef IntVector::iterator IntVectorIt ;

IntVector Numbers(VECTOR_SIZE) ;
IntVectorIt start, end, it, location ;

Numbers[0] = 4 ;
Numbers[1] = 10;
Numbers[2] = 10 ;
Numbers[3] = 30 ;
Numbers[4] = 69 ;
Numbers[5] = 70 ;
Numbers[6] = 96 ;
Numbers[7] = 100;

start = Numbers.begin() ;
end = Numbers.end() ;

location = lower_bound(start, end, 10,greater <int>()) ;

cout << *location<< endl ;
}

为什么输出的是 -33686019?
实验过location-start等于8?不明白为什么?向各位大虾请教.

[解决办法]
应该用
less<int>
[解决办法]
lower_bound(...., op)
op是容器中数据的排序标准,所以容器中的排序顺序应该保证与op一致。

greater<int>()改成less<int>或者不要。

读书人网 >C++

热点推荐