我實現了一個二進制搜索這樣的:C++ STL二進制搜索(LOWER_BOUND,UPPER_BOUND)
typedef std::vector<Cell>::iterator CellVectorIterator;
typedef struct _Point {
char x,y;
} CoordinatePoint;
typedef struct _Cell {
...
CoordinatePoint coordinates;
} Cell;
struct CellEqualityByCoordinates
{
bool
operator()(const Cell& cell1, const Cell& cell2) const
{ return cell1.coordinates.x == cell2.coordinates.x && cell1.coordinates.y == cell2.coordinates.y; }
};
CellVectorIterator FindCellByCoordinates (CellVectorIterator first, CellVectorIterator last, const Cell &val)
{
return std::upper_bound(first, last, val, CellEqualityByCoordinates());
}
不過,這並不總能找到一個值。
這是什麼問題?
http://stackoverflow.com/questions/18958015/stdsort-giving-very-strange-results/18958178#18958178 – billz