Template class Komplex with concepts
main.cpp
Go to the documentation of this file.
1 // Klasse Komplex wird erweitert; Templates
2 // operator+ wird aus operator += abgeleitet
3 // Vergleichsoperatoren: < , == und daraus abgeleitet >
4 // ( nur zur Demo: Vergleichsoperatoren fuer komplexe Zahlen sind nicht transitiv !!)
5 //#include "komplex.h"
6 #include "komplex2.h"
7 #include <algorithm> // copy, sort
8 #include <iostream>
9 #include <iterator> // ostream_iterator
10 #include <vector>
11 using namespace std;
12 
13 template <class T>
14 ostream &operator<<(ostream &s, const vector<T> &v)
15 {
16 // for (auto it=v.begin(); it!=v.end; ++it) cout << *it << " ";
17  copy(v.begin(), v.end(), ostream_iterator<T, char>(s, " "));
18  return s;
19 }
20 
21 template <class T>
22 bool islargerequal(T a, T b)
23 {
24  return !(a < b);
25 }
26 
27 int main()
28 {
29  const Komplex<double> a(3.2, -1.1); // Konstruktor Komplex(double,double)
30  const Komplex<double> b(4, -1); // Konstruktor Komplex(double,double)
31  Komplex<double> c; // Konstruktor Komplex() wird benoetigt
32 
33  c = a + b; // OK: a.operator+(const Komplex&)
34 
35  cout << a << endl; // Ausgabeoperator
36  cout << c << endl;
37 
38  Komplex<double> dd(-3.2);
39  dd += a; // OK: a.operator+(const Komplex&)
40  cout << dd << endl;
41 
42  cout << (dd < a) << endl;
43  cout << (dd == a) << endl;
44  cout << (dd > a) << endl;
45 
46  vector<Komplex<float>> vv = { {3.0F, -1.0F}, {3.0F, -3.0F}, {1.2F, -4.F}, {4.3F, -1.F} };
47  cout << "vv : " << vv << endl;
48  sort(vv.begin(), vv.end()); // requires operator<, ans operator= (for vector-container)
49  cout << "vv : " << vv << endl;
50 
51  sort(vv.begin(), vv.end(), islargerequal<Komplex<float>>);
52  cout << "vv : " << vv << endl;
53 
54  // order wrt. abs(), with lambda function
55  sort(vv.begin(), vv.end(), //islargerequal<Komplex<float>>
56  [] (auto const &aa, auto const &bb) -> bool
57  { return abs(aa) < abs(bb); }
58  );
59  cout << "vv: abs : " << vv << endl;
60 
61 
62  auto it = find(vv.begin(), vv.end(), Komplex<float>(1.22F, -4.0F) );
63  if (it != vv.end()) {cout << " found " << *it << endl;}
64 
65  Komplex<long double> lda(1.22L, -4.0L); cout << lda << endl;
66  //Komplex<int> ia(-1,2); cout << ia << endl;
67 
68  // https://stackoverflow.com/questions/53557649/how-do-i-check-for-c20-support-what-is-the-value-of-cplusplus-for-c20
69  #if __cplusplus >= 202002L
70  cout << " C++20 support" << endl;
71  #else
72  cout << " C++ version: " << __cplusplus << endl;
73  #endif
74 
75  Komplex<string> hh("n","a");
76 
77 
78  return 0;
79 }
ostream & operator<<(ostream &s, const vector< T > &v)
Definition: main.cpp:14
bool islargerequal(T a, T b)
Definition: main.cpp:22
int main()
Definition: main.cpp:27