Advertisement
alansam

Using the spaceship operator.

Jun 16th, 2021
1,708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <compare>
  3.  
  4. int main(int argc, char const * argv[]) {
  5.   int a;
  6.   int b;
  7.   std::cout << "value of a? ";
  8.   std::cout.flush();
  9.   std::cin >> a;
  10.   std::cout << "value of b? ";
  11.   std::cout.flush();
  12.   std::cin >> b;
  13.   std::cout << std::endl;
  14.  
  15.   auto cmp = a <=> b;
  16.  
  17.   if (std::is_eq(cmp)) {
  18.     std::cout << "a [" << a << "] is equal to b [" << b << ']' << '\n';
  19.   }
  20.   else if (std::is_lt(cmp)) {
  21.     std::cout << "a [" << a << "] is less than b [" << b << ']' << '\n';
  22.   }
  23.   else {
  24.     std::cout << "a [" << a << "] is greater than b [" << b << ']' << '\n';
  25.   }
  26.   std::cout << std::endl;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement