aboutsummaryrefslogtreecommitdiff
path: root/C++/Threeway Comparison.wiki
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-09-06 12:24:03 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-09-06 12:24:03 +0200
commit95bfddc4329119535cf2fb47bef3c1b87f61e411 (patch)
tree69c5b24bfe8fb7028450aaed7f0abf38f40329da /C++/Threeway Comparison.wiki
parentons 31 aug 2022 17:37:49 CEST (diff)
downloadwiki-public-95bfddc4329119535cf2fb47bef3c1b87f61e411.tar.gz
wiki-public-95bfddc4329119535cf2fb47bef3c1b87f61e411.tar.xz
tis 6 sep 2022 12:24:03 CEST
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;
+}
+}}}