-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.cpp
69 lines (60 loc) · 1.34 KB
/
set.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
#include "set.h"
using namespace std;
void Set::insert(int e)
{
unsigned int index;
if(!search(e,index)){
_vec.push_back(e);
cnt_evens=evens();
}
else{
throw ExistingElementException();
}
}
void Set::remove(int d)throw (std::exception){
unsigned int ind;
if(search(d,ind)){
_vec[ind] = _vec[_vec.size()-1];
_vec.pop_back();
cnt_evens=evens();
}
else{
throw NonExistingElementException();
}
}
bool Set::isIn(int c)const{
unsigned int index;
return search(c,index);
}
int Set::rand_elem() const throw (std::exception){
if(!isEmpty()){
srand((unsigned) (time(0)));
int rand_num = rand() % (_vec.size());
return _vec[rand_num];
}
else{
throw EmptySetException();
}
}
int Set::evens() const{
int cnt=0;
for(unsigned int i=0; i<_vec.size(); i++){
if(_vec[i] % 2==0) cnt++;
}
return cnt;
}
bool Set::search(int k, unsigned int &ind) const{
bool g = false;
for(unsigned int i=0; !g && i<_vec.size(); i++){
g = (_vec[i]==k);
ind = i;
}
return g;
}
vector<int> Set::getSet() const{
vector<int> v;
for(unsigned i=0; i<_vec.size(); ++i){
v.push_back(_vec[i]);
}
return v;
}