Template class Komplex alt
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 <algorithm>
// copy, sort
7
#include <iostream>
8
#include <iterator>
// ostream_iterator
9
#include <vector>
10
using namespace
std;
11
12
template
<
class
T>
13
ostream &
operator<<
(ostream &s,
const
vector<T> &v)
14
{
15
// for (auto it=v.begin(); it!=v.end; ++it) cout << *it << " ";
16
copy(v.begin(), v.end(), ostream_iterator<T, char>(s,
" "
));
17
return
s;
18
}
19
20
template
<
class
T>
21
bool
islargerequal
(T a, T b)
22
{
23
return
!(a < b);
24
}
25
26
int
main
()
27
{
28
const
Komplex<double>
a(3.2, -1.1), b(4, -1);
// Konstruktor Komplex(double,double)
29
Komplex<double>
c;
// Konstruktor Komplex() wird benoetigt
30
31
c = a + b;
// OK: a.operator+(const Komplex&)
32
33
cout << a << endl;
// Ausgabeoperator
34
cout << c << endl;
35
36
Komplex<double>
dd(-3.2);
37
dd += a;
// OK: a.operator+(const Komplex&)
38
cout << dd << endl;
39
40
cout << (dd < a) << endl;
41
cout << (dd == a) << endl;
42
cout << (dd > a) << endl;
43
44
vector<Komplex<float>> vv = { {3.0f, -1.0f}, {3.0f, -3.0f}, {1.2f, -4.f}, {4.3f, -1.f} };
45
cout <<
"vv : "
<< vv << endl;
46
sort(vv.begin(), vv.end());
// requires operator<, ans operator= (for vector-container)
47
cout <<
"vv : "
<< vv << endl;
48
49
sort(vv.begin(), vv.end(),
islargerequal
<
Komplex<float>
>);
50
cout <<
"vv : "
<< vv << endl;
51
52
// order wrt. abs(), with lambda function
53
sort(vv.begin(), vv.end(),
//islargerequal<Komplex<float>>
54
[] (
const
Komplex<float>
&aa,
const
Komplex<float>
&bb) ->
bool
55
{ return abs(aa) < abs(bb); }
56
);
57
cout <<
"vv: abs : "
<< vv << endl;
58
59
60
auto
it = find(vv.begin(), vv.end(),
Komplex<float>
(1.22f, -4.0f) );
61
if
(it != vv.end()) cout <<
" found "
<< *it << endl;
62
63
//
64
// cout << "Komplex with string members: ";
65
// Komplex<string> ss("Real","Imag");
66
// cout << ss << endl;
68
69
return
0;
70
}
Komplex
Definition:
komplex.h:14
komplex.h
operator<<
ostream & operator<<(ostream &s, const vector< T > &v)
Definition:
main.cpp:13
islargerequal
bool islargerequal(T a, T b)
Definition:
main.cpp:21
main
int main()
Definition:
main.cpp:26
main.cpp
Generated by
1.9.1