Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
993 B

  1. #include "denpre.h"
  2. #include "util.h"
  3. template <class T>
  4. class less
  5. {
  6. public:
  7. BOOL operator()(const T &a, const T &b)
  8. {
  9. return a < b;
  10. }
  11. };
  12. int vec[10] = { 33, 251, 253, 254, 263, 272, 274, 302, 305, 307 };
  13. void PrintPair(int value, int *pLB, int *pUB)
  14. {
  15. if (pLB == NULL && pUB == NULL)
  16. printf("Array is Empty or Bug in bsearch!\n");
  17. else if (pLB == NULL)
  18. printf("%d < %d\n", value, *pUB);
  19. else if (pUB == NULL)
  20. printf("%d > %d\n", value, *pLB);
  21. else
  22. printf("%d <= %d <= %d\n", *pLB, value, *pUB);
  23. }
  24. void main()
  25. {
  26. int *pLB, *pUB;
  27. bsearch(&vec[0], &vec[10], 253, less<int>(), &pLB, &pUB);
  28. PrintPair(253, pLB, pUB);
  29. bsearch(&vec[0], &vec[10], 267, less<int>(), &pLB, &pUB);
  30. PrintPair(267, pLB, pUB);
  31. bsearch(&vec[0], &vec[10], 399, less<int>(), &pLB, &pUB);
  32. PrintPair(399, pLB, pUB);
  33. bsearch(&vec[0], &vec[10], 2, less<int>(), &pLB, &pUB);
  34. PrintPair(2, pLB, pUB);
  35. bsearch(&vec[0], &vec[0], 39, less<int>(), &pLB, &pUB);
  36. PrintPair(39, pLB, pUB);
  37. }