Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. // binary_search example
  2. #include <iostream> // std::cout
  3. #include <algorithm> // std::binary_search, std::sort
  4. #include <vector> // std::vector
  5.  
  6. bool myfunction (int i,int j) { return (i<j); }
  7.  
  8. int main () {
  9. int myints[] = {1,2,3,4,5,4,3,2,1};
  10. std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1
  11.  
  12. // using default comparison:
  13. std::sort (v.begin(), v.end());
  14.  
  15. std::cout << "looking for a 3... ";
  16. if (std::binary_search (v.begin(), v.end(), 3))
  17. std::cout << "found!\n"; else std::cout << "not found.\n";
  18.  
  19. // using myfunction as comp:
  20. std::sort (v.begin(), v.end(), myfunction);
  21.  
  22. std::cout << "looking for a 6... ";
  23. if (std::binary_search (v.begin(), v.end(), 6, myfunction))
  24. std::cout << "found!\n"; else std::cout << "not found.\n";
  25.  
  26. return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement