Class Komplex + STL
main.cpp
Go to the documentation of this file.
1 // Klasse Komplex
2 // ohne automatisch, implizit generierte Methoden
3 // mit Operator+
4 #include "komplex.h"
5 #include <algorithm> // sort
6 #include <iostream>
7 #include <vector>
8 using namespace std;
9 
10 //#define PRINT(x) #x << " : " << v
11 
17 std::ostream& operator<<(std::ostream& s, const std::vector<Komplex>& rhs);
18 
19 int main()
20 {
21  const Komplex a(3.2,-1.1), b(4,-1); // Konstruktor Komplex(double,double)
22  Komplex c; // Konstruktor Komplex() wird benoetigt
23 
24  c = a+b; // OK: a.operator+(const Komplex&)
25 
26  cout << a << endl; // Ausgabeoperator
27  cout << c << endl;
28 
29  double dd(-3.2);
30  // OK: a.operator+(const Komplex&)
31  c = a + Komplex(dd,0.0);// explizites Casting double --> Komplex durch Konstruktor Komplex(double)
32  c = a + dd; // implizites Casting durch Konstruktor Komplex(double)
33  cout << c << endl;
34 
35  c = dd+a; // Achtung: keine Methode operator+(const Komplex&)
36  // fuer double verfuegbar.
37  // Daher Funktion operator+(double, const Komplex&) noetig
38  cout << c << endl;
39 
40  //------------------------------------------------------------------
41  vector<Komplex> v{ {-1,2.0}, {3.0}, {0.0, -4}, {1,1} };
42  cout << "original " << v << endl;
43  //cout << PRINT(v) << endl;
44 
45  sort(v.begin(),v.end()); // Komplex:operator<()
46  cout << "nach operator<() " << v << endl;
47 
48  sort(v.begin(),v.end(), // Lambda-Funktion
49  [](Komplex const &lhs, Komplex const &rhs) -> bool
50  { return lhs.abs()<rhs.abs(); }
51  );
52  cout << "nach lambda-Fkt. " << v << endl;
53 
54 
55 
56  return 0;
57 }
58 
59 ostream& operator<<(ostream& s, const vector<Komplex>& rhs)
60 {
61  for (auto const &vk: rhs )
62  {
63  s << vk << " ";
64  }
65  return s;
66 }
Definition: komplex.h:8
std::ostream & operator<<(std::ostream &s, const std::vector< Komplex > &rhs)
int main()
Definition: main.cpp:19