First Class
main.cpp
Go to the documentation of this file.
1
// Klasse Student
2
// Output-Operator (friend); Mehrere Studien
3
// Nutzung der STL
4
#include "
student.h
"
5
#include <algorithm>
6
#include <iostream>
7
#include <vector>
8
9
using namespace
std;
10
11
19
bool
num_studs
(
const
Student
& lhs,
const
Student
& rhs);
20
21
28
struct
Check_SKZ
29
{
30
private
:
31
int
_cSKZ;
32
public
:
34
Check_SKZ
(
int
cSKZ): _cSKZ(cSKZ){}
36
bool
operator()
(
const
Student
&lhs)
const
{
return
lhs.
Enrolled4Study
(_cSKZ);}
37
} ;
38
39
int
main
()
40
{
41
cout <<
"Hello world!"
<< endl;
42
43
vector<Student> gg{ {
"Tyson"
,
"8956256215"
, 421 },
// uses Student(..., int)
44
{
"Strude"
,
"0771871827"
, {865, 421, 666} },
// uses Student(..., vector<int>)
45
{
"Neuner"
,
"997216726"
, {732, 865} }
// uses Student(..., vector<int>)
46
} ;
// C++-11 initialization list
47
48
// cout << " -- at -- " << endl;
49
// for (unsigned int i=0; i<gg.size(); ++i)
50
// {
51
// cout << gg.at(i) << endl;
52
// }
53
//
54
// cout << " -- iterator -- " << endl;
55
// for (auto pi=gg.begin(); pi!=gg.end(); ++pi)
56
// {
57
// cout << *pi << endl;
58
// }
59
60
cout <<
" -- C++11 range-for -- "
<< endl;
61
for
(
auto
pi: gg)
62
{
63
cout << pi << endl;
64
}
65
66
// sort with respect to names lexicographically
67
sort(gg.begin(),gg.end());
// requires method Student::operator<(const Student&) that compares the names lexicographically
68
69
cout <<
" -- nach name sort -- "
<< endl;
70
for
(
auto
pi: gg) { cout << pi << endl; }
71
72
sort(gg.begin(),gg.end(),
num_studs
);
// uses the compare function bool num_studs(const Student&,const Student&)
73
//sort(gg.begin(),gg.end(), [](const Student &lhs, const Student &rhs) {return lhs.Num_SKZ() < rhs.Num_SKZ();} ); // via Lambda-Funktion
74
cout <<
" -- nach SKZ sort -- "
<< endl;
75
for
(
auto
pi: gg) { cout << pi << endl; }
76
77
// find first student with study 865
78
const
int
cSKZ=865;
79
auto
it = find_if(gg.cbegin(),gg.cend(),
Check_SKZ
(cSKZ));
// requires unary function (via functor)
80
cout << endl <<
" ## first student with SKZ "
<< cSKZ <<
" : "
<< *it << endl;
81
//
82
// count the number students of study 865
83
int
ns = count_if(gg.cbegin(),gg.cend(),
// requires unary function (via lambda function)
84
[cSKZ] (
Student
const
& a) {return a.Enrolled4Study(cSKZ);}
85
);
86
cout << endl << ns <<
" students have SKZ "
<< cSKZ << endl;
87
88
return
0;
89
}
90
91
92
bool
num_studs
(
const
Student
& lhs,
const
Student
& rhs)
93
{
94
return
lhs.
Num_SKZ
() < rhs.
Num_SKZ
();
// compares the number of studies
95
}
Student
Definition:
student.h:11
Student::Enrolled4Study
bool Enrolled4Study(const int cSKZ) const
Checks whether a student is enrolled in study cSKZ.
Definition:
student.h:131
Student::Num_SKZ
int Num_SKZ() const
Definition:
student.h:96
num_studs
bool num_studs(const Student &lhs, const Student &rhs)
binary boolean function: Compares the number of studies of two students
Definition:
main.cpp:92
main
int main()
Definition:
main.cpp:39
Check_SKZ
Functor for passing a parameter cSKZ to an comparison operator.
Definition:
main.cpp:29
Check_SKZ::Check_SKZ
Check_SKZ(int cSKZ)
< Constructor for functor
Definition:
main.cpp:34
Check_SKZ::operator()
bool operator()(const Student &lhs) const
Definition:
main.cpp:36
student.h
main.cpp
Generated by
1.9.1