-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
167 lines (134 loc) · 3.78 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Kontenery
#include <vector> // Tablica
#include <set> // Zbiór
#include <map> // Słownik
// Adaptery
#include <queue> // Kolejka
#include <stack> // Stos
#include <iostream>
#include <stdio.h>
#include "Slav.h"
#define REPORT_ADAPTERS showMeAdapterSizes(queueOfSlavs,stackOfSlavs)
#define REPORT_CONTAINERS showMeContainerSizes(vectorOfSlavs,setOfSlavs,mapOfSlavs)
using namespace std;
void showMeContainerSizes(vector <Slav *>, set <Slav *>, map <Slav *, Slav*>);
void showMeAdapterSizes(queue <Slav *>, stack <Slav *>);
void containers(Slav *, int);
void adapters(Slav *, int);
void Show_Sex(Slav*, int);
int main(int argc, char const *argv[])
{
srand(time(NULL));
int n = 2 * atoi(argv[1]);
Slav *slavs = new Slav[n];
cout << "# Generated Slavs" << endl;
for (int i = 0; i < n; ++i)
cout << slavs[i].description() << endl;
containers(slavs, n);
adapters(slavs, n);
Show_Sex(slavs, n);
delete [] slavs;
}
void containers(Slav * slavs, int n)
{
vector <Slav *> vectorOfSlavs;
set <Slav *> setOfSlavs;
map <Slav *, Slav *> mapOfSlavs;
printf("# Containers\n");
REPORT_CONTAINERS;
printf("## vector\n");
// Umieść Słowian w losowej kolejności w wektorze.
vectorOfSlavs.push_back(slavs);
for(int i=1 ; i<n ; i++)
{
vectorOfSlavs.insert(vectorOfSlavs.begin() + rand()%vectorOfSlavs.size(), &slavs[i]);
}
// Wykorzystując iterator i funkcję description(), wyświetl wszystkich Słowian w wektorze
vector <Slav *>::iterator itr;
for( itr = vectorOfSlavs.begin();itr != vectorOfSlavs.end(); itr++)
{
cout << (*itr) -> description() << endl;
}
REPORT_CONTAINERS;
printf("## set\n");
// Przenieś wszystkich Słowian z wektoru do zbioru.
for(int i = n-1; i>=0 ; i--)
{
setOfSlavs.insert(vectorOfSlavs[i]);
vectorOfSlavs.pop_back();
}
REPORT_CONTAINERS;
printf("## map\n");
// Stwórz słownik tworzący pary Słowian, z tych znajdujących się w zbiorze, czyszcząc zbiór
set< Slav* >::iterator itr_s = setOfSlavs.begin();
set< Slav* >::iterator itr_removal_s = setOfSlavs.begin();
while(itr_s != setOfSlavs.end())
{
while(itr_s !=setOfSlavs.end())
{
mapOfSlavs[*itr_s++] = *itr_s++;
setOfSlavs.erase(itr_removal_s++);
setOfSlavs.erase(itr_removal_s++);
}
}
// Wykorzystując iterator, wyświetl wszystkie pary Słowian
map <Slav *, Slav*>::iterator itr_m = mapOfSlavs.begin();
while(itr_m != mapOfSlavs.end())
{
cout << (itr_m -> first) -> description() << " -> " <<(itr_m -> second) -> description() << endl;
itr_m++;
}
REPORT_CONTAINERS;
}
void adapters(Slav * slavs, int n)
{
queue <Slav *> queueOfSlavs;
stack <Slav *> stackOfSlavs;
printf("# Adapters\n");
REPORT_ADAPTERS;
printf("## queue\n");
// Umieść Słowian w kolejce.
for(int i = 0 ; i < n; i++)
{
queueOfSlavs.push(&slavs[i]);
}
REPORT_ADAPTERS;
printf("## stack\n");
// Przenieś Słowian z kolejki do stosu.
while(queueOfSlavs.empty() == 0)
{
stackOfSlavs.push(queueOfSlavs.front());
queueOfSlavs.pop();
}
REPORT_ADAPTERS;
// Wyświetl Słowian zdejmowanych ze stosu.
while(stackOfSlavs.empty() == 0)
{
cout << stackOfSlavs.top() -> description() << endl;
stackOfSlavs.pop();
}
REPORT_ADAPTERS;
}
void Show_Sex(Slav *slavs, int n)
{
map < gender, vector< Slav* > > mapOfGender;
for(int i = 0; i<n ; i++)
{
(slavs[i].Sex()== man ) ? mapOfGender[man].push_back(slavs+i) : mapOfGender[woman].push_back(slavs+i);
}
}
void showMeContainerSizes(vector <Slav *> vector, set <Slav *> set, map <Slav *, Slav*> map)
{
printf("[vector_size = %lu, set_size = %lu, map_size = %lu, existingSlavs = %i]\n",
vector.size(),
set.size(),
map.size(),
Slav::counter());
}
void showMeAdapterSizes(queue <Slav *> queue, stack <Slav *> stack)
{
printf("[queue_size = %lu, stack_size = %lu, existingSlavs = %i]\n",
queue.size(),
stack.size(),
Slav::counter());
}