From 95bfddc4329119535cf2fb47bef3c1b87f61e411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 6 Sep 2022 12:24:03 +0200 Subject: tis 6 sep 2022 12:24:03 CEST --- C++/Threeway Comparison.wiki | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 C++/Threeway Comparison.wiki (limited to 'C++/Threeway Comparison.wiki') 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 +#include +#include + +int str_to_int(const char* s) { + int ret; + std::basic_istringstream 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; +} +}}} -- cgit v1.2.3