-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceship_demo.cpp
More file actions
48 lines (36 loc) · 1.74 KB
/
spaceship_demo.cpp
File metadata and controls
48 lines (36 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Demos MSVC STL implementation of "Adding <=> to the Library" Proposal: https://wg21.link/P1614R2
#include <iostream>
#include <string>
#include <vector>
void spaceship_in_library_types_demo()
{
// Set boolalpha format flag so bool values are represented by their textual representation: true or false
std::boolalpha(std::cout);
std::cout << "\nOperator <=> in the library demo:\n";
std::cout << "\nOperator <=> in vector container:\n";
std::vector<int> a1(3, 10);
std::vector<int> a2(3, 10);
std::vector<int> b1(2, 20);
std::cout << ((a1 <=> a2) == 0) << std::endl; // true
std::cout << ((a1 <=> b1) < 0) << std::endl; // true
std::cout << ((b1 <=> a1) == 0) << std::endl; // false
std::cout << "\nOperator <=> in vector iterator:\n";
std::cout << ((a1.begin() <=> a1.begin()) == 0) << std::endl; // true
std::cout << ((a1.begin() <=> a1.end()) < 0) << std::endl; // true
std::cout << ((b1.begin() <=> b1.end()) == 0) << std::endl; // false
std::cout << "\nOperator <=> in string:\n";
const std::string s1 = "abc";
const std::string s2 = "abc";
const std::string s3 = "abcde";
const std::string s4 = "ab";
const std::string s5 = "abd";
const std::string s6 = "abb";
// All should return true
std::cout << ((s1 <=> s2) == std::strong_ordering::equivalent) << std::endl;
std::cout << ((s1 <=> s3) == std::strong_ordering::less) << std::endl;
std::cout << ((s1 <=> s4) == std::strong_ordering::greater) << std::endl;
std::cout << ((s1 <=> s5) == std::strong_ordering::less) << std::endl;
std::cout << ((s1 <=> s6) == std::strong_ordering::greater) << std::endl;
}