MySet容器实现

MySet .hpp

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
#ifndef _myset
#define _myset
#include<iostream>
#include<set>
using namespace std;


//基于set容器来实现MySet自定义容器
//参数1为 里面存的数据类型 参数2 用哪种容器来实现,并且默认为set容器
/*
* 注:
在模板(template)中使用一个嵌套从属类型名称, 需要在前一个位置, 添加关键字。
就是在这个容器中使用另一个容器,当用类名调用的时候要加上前缀typename
*/
template<class _ty,class _container = set<_ty>>
class MySet
{
public:

//重命名——便于书写
typedef MySet<_ty, _container> _MySet;




//用基于的容器来初始化咱们这个MySet容器
MySet() :c()
{

}
~MySet()
{

}
//拷贝构造函数-根据传进来的MySet容器
MySet(const _MySet& _Right) :c(_Right.c)
{

}
//拷贝构造函数-根据传进来的容器种类
MySet(const _container* _Left) :c(_Left)
{

}
//重载赋值运算符
_MySet& operator=(const _MySet& _Right)
{
this->c = _Right.c;
return *this;
//返回引用-内容-所以要*this指针得到内容并返回
}
//插入元素-调用set容器-用返回值判断是否插入成功
bool insert(const _ty& _Val)
{
pair<typename _container::iterator, bool>ret = c.insert(_Val);
if (ret.second)
{
cout << _Val << "插入成功" << endl;
return true;
}
else
{
cout << _Val << "插入失败" << endl;
return false;
}
}

//检查容器是否为空-直接调用set容器接口
bool empty()const
{
return c.empty;
}
//删除操作-直接调用set容器接口来判断
bool erase(const _ty& _Val)
{
if (c.erase(_Val) > 0)//存在并删除成功返回删除元素的个数,反之返回0
{
return true;
}
return false;
}
//当前容器长度-直接调用set容器接口
int size()const
{
return c.size();
}
//得到当前容器的最大值-因为是有以set容器实现的,第一个是最小的,最后一个是最大的
//返回对组的用处在这里体现了,对组中可以存一个值+一个bool,来判断是否成功。
pair<_ty, bool> GetMax()const
{
pair<_ty, bool>ret;
typename _container::iterator max = c.end();
//先判断容器中是否有东西
if (c.size() > 0)
{
ret.first = *(--max);
ret.second = true;
return ret;
}
else
{
ret.second = false;
return ret;
}
}
//得到最小值
//核心容器的::value_type 和_ty-传进来的数据类型相同
pair<_ty, bool> GetMin()const
{
pair<_ty, bool>ret;
typename _container::iterator min = c.begin();
//判断是否存在
if (min != c.end())
{
ret.first = *c.begin();
ret.second = true;
return ret;
}
else
{
ret.second = false;
return ret;
}

}
void print()const
{
for (typename _container::iterator it = c.begin(); it != c.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}

protected:
//基于哪种容器实现
_container c;
};

#endif

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
#include<iostream>
#include"MySet.hpp"
using namespace std;
int main(void)
{
MySet<int>ms1;
ms1.insert(5);
ms1.insert(15);
ms1.insert(25);

pair<int,bool>ret = ms1.GetMax();
if (ret.second)
{
cout <<"找到了,最大值是:" << ret.first << endl;
}
else
{
cout << "找不到" << endl;
}

pair<int, bool>ret2 = ms1.GetMin();
if (ret2.second)
{
cout << "找到了,最小值是:" << ret2.first << endl;
}
else
{
cout << "找不到" << endl;
}

ms1.print();

MySet<int>ms2(ms1);
ms2.print();

ms2.erase(15);

MySet<int>ms3;
ms3 = ms2;
ms3.print();

return 0;
}

SizeFilter容器实现

SizeFilter.hpp

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
#ifndef _SIZEFILTER
#define _SIZEFILTER

#include<iostream>
#include<set>


template<class _Ty,class _Container = set<_Ty>>//第二个模板参数是默用set容器来实现这个新容器的功能
class sizeFilter
{
public:
typedef sizeFilter<_Ty, _Container> _Myt;
/*
在模板(template)中使用一个嵌套从属类型名称, 需要在前一个位置, 添加关键字。
就是在这个容器中使用另一个容器,当前类容器时模板,要加上前缀typename
*/

//用核心容器中的类型(用set容器中的size_type 来当我们这个新容器的size_type)
typedef typename _Container::size_type size_type;
typedef typename _Container::value_type value_type;
sizeFilter() :c()
{
//默认构造函数。初始化内置容器
}

sizeFilter(const _Myt& _Right) :c(_Right.c)
{
//构造函数,通过指定特定的sizeFilter容器构造
}

sizeFilter(const _Container& _Cont) :c(_Cont)
{
//构造函数,通过指定特定的容器构造

}
_Myt& operator= (const _Myt& _Right)
{
//使用sizefilter给另一个sizefilter赋值
c = _Right.c;
return (*this);
}

bool empty()const
{
//用核心容器的功能
return c.empty();
}

size_type size()const
{
return c.size();
}

bool insert(const value_type& _Val)
{
_Container::iterator ci = c.insert(c.begin(), _Val);
if (ci != c.end())
{
std::cout << "插入:" << _Val << "成功" << std::endl;
return true;
}
std::cout << "插入:" << _Val << "失败" << std::endl;
}

bool erase(const value_type& _Val)
{
if (c.erase(_Val) > 0)
{
return true;
}
return false;
}

//set容器自动排序,默认升序排列,第一个为min,最后一个为max
std::pair<value_type, bool> getMin()//获取最小值
{
std::pair<value_type, bool> ret;
typename _Container::iterator min = c.begin();
if (min != c.end())//存在最小值
{
ret.first = *min;
ret.second = true;
return ret;
}
ret.second = false;
return ret;
}

std::pair<value_type, bool> getMax()//获取最大值
{
std::pair<value_type, bool>ret;
typename _Container::iterator max = c.end();
if (c.size() > 0)//只要容器不是空的,里面就能取到最大值
{
ret.first = *(--max);
ret.second = true;
return ret;
}
ret.second = false;
return ret;
}


protected:
_Container c;//基于哪个容器实现的,这个c就是什么类型的容器
};

#endif

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
#include<iostream>
#include"SizeFilter.hpp"
#include<string>
#include<set>
using namespace std;
int main(void)
{
sizeFilter<int> sf;
//插入
sf.insert(5);


pair<sizeFilter<int>::value_type, bool>ret = sf.getMax();
if (ret.second)
{
cout <<"最大值是:" <<ret.first<< endl;//看迭代器里面是什么内容来决定操作
}
else
{
cout << "没找到" << endl;
}


pair<sizeFilter<int>::value_type, bool>ret2 = sf.getMin();
if (ret2.second)
{
cout << "最小值是:" << ret.first << endl;
}
else
{
cout << "没找到" << endl;
}


return 0;
}