fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef int T;
  6.  
  7. class tutorial_t: vector< T >
  8. {
  9. T val; iterator it, iu;
  10.  
  11. void example( const function< iterator() >& bound_function,
  12. const function< bool( const T&, const T& ) >& bound_operator,
  13. const string& function_name,
  14. const string& operator_name ) const
  15. {
  16. iterator iv = bound_function(); cout << function_name << "( begin(), end(), " << val << " ): ";
  17.  
  18. if ( iv != iu )
  19. cout << "Found " << *iv << " at position " << ( iv - it ) << endl;
  20. else
  21. cout << "Not found! all items are " << operator_name << ' ' << val << endl;
  22.  
  23. assert( iv == iu or not bound_operator( *iv, val ) );
  24. }
  25.  
  26. public:
  27.  
  28. tutorial_t()
  29. {
  30. size_t n; cin >> n >> val, resize( n ), it = begin(), iu = end();
  31.  
  32. for( auto& a: *this )
  33. cin >> a;
  34.  
  35. sort( it, iu ), cout << "sorted_vector(" << n << "):";
  36.  
  37. for( auto a: *this )
  38. cout << ' ' << a;
  39.  
  40. cout << endl, assert( is_sorted( it, iu ) );
  41.  
  42. example( [&]() { return lower_bound( it, iu, val ); }, less< T >(), "lower_bound", "<" ),
  43. example( [&]() { return upper_bound( it, iu, val ); }, less_equal< T >(), "upper_bound", "<=" );
  44. }
  45. };
  46.  
  47. int main()
  48. {
  49. ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );
  50.  
  51. tutorial_t tutorial;
  52. }
  53.  
Success #stdin #stdout 0s 15240KB
stdin
8 20
10 20 30 30 20 10 10 20
stdout
sorted_vector(8): 10 10 10 20 20 20 30 30
lower_bound( begin(), end(), 20 ): Found 20 at position 3
upper_bound( begin(), end(), 20 ): Found 30 at position 6