aboutsummaryrefslogtreecommitdiff
path: root/C++/Threeway Comparison.wiki
diff options
context:
space:
mode:
Diffstat (limited to 'C++/Threeway Comparison.wiki')
-rw-r--r--C++/Threeway Comparison.wiki43
1 files changed, 43 insertions, 0 deletions
diff --git a/C++/Threeway Comparison.wiki b/C++/Threeway Comparison.wiki
new file mode 100644
index 0000000..58edd84
--- /dev/null
+++ b/C++/Threeway Comparison.wiki
@@ -0,0 +1,43 @@
+Compile with `c++ -std=c++=20 main.cpp`.
+
+Tests the spaceship operator.
+
+{{{c++
+#include <iostream>
+#include <compare>
+#include <sstream>
+
+int str_to_int(const char* s) {
+ int ret;
+ std::basic_istringstream<char> is(s);
+ is >> ret;
+ return ret;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 3) {
+ std::cerr << "Give at least 2 arguments" << std::endl;
+ return 1;
+ }
+
+ int a, b;
+
+ a = str_to_int(argv[1]);
+ b = str_to_int(argv[2]);
+
+ std::cout << a << " <=> " << b << std::endl;
+ std::strong_ordering result = a <=> b;
+ if (std::is_lt(result)) {
+ std::cout << "LT";
+ } else if (std::is_gt(result)) {
+ std::cout << "GT";
+ } else if (std::is_eq(result)) {
+ std::cout << "EQ";
+ } else {
+ std::cout << "error";
+ }
+
+ std::cout << std::endl;
+ return 0;
+}
+}}}