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