认识STL

STL的概述

STL采用泛型思想,把C中所用到的所有的数据结构,按照一定的标准,全部封装成了一个个类模板。也被称为数据容器。

STL就是用来解决容器中数据元素的操作的问题的。并且他按排标准统一封装了操作容器元素的算法,即一个个的函数模板。

为了配合统一的算法去操作不同的容器,他又按标准统一封装了不同的迭代器,即一个个不同类型的具有指针特性的类模板。

所以容器,算法,迭代器是整个STL的核心。

三者的关系如图所示:

有了统一的数据结构,即容器,有了统一的算法,每一个容器都使用各自的不同的迭代器,从而实现了对数据结构元素的标准操作。

STL标准模板库都有什么

STL的知识结构如图:

容器

  • 常用的数据结构,C++重新封装成了 vector(动态数组),list(链表),deque(双向队列),set(集合) ,map(映射表)等,来实现存放数据,其中 set,map双叫关联容器 。其中的栈,单端队列,优先队列,又都是线性数据结构改造之后的具有各种特性的数据结构,又叫做容器适配器。
  • 序列式容器强调值的排序,序列式容器中的每个元素均有固定的位置,除非用删除或插入的操作改变这个位置。vector容器,deque容器,List容器等。
  • 关联式容器是非线性的树结构,更准确的说是二叉树结构,各元素之间没有严格的物理上的顺序关系,也就是说元素在容器中并没有保存元素入容器时的逻辑顺序 。关联式容器另一个显著特点是:在值中选择一个值作为关键字Key,这个关键字对值起到了索引的作用,方便查找。Set/mulitiset容器 Map/multimap容器,并且容器是可以嵌套使用的。

算法

algorithom头文件中的定义相关的操作的一系列的函数模板

STL收录的算法经过了数学上的效能分析与证明,且是经过商业运行考验过的,是极具复用价值 的,包括常用的排序,查找等。

​ 算法又可为为两种,质变算法,与非质变算法。

​ 何为质变算法:是指运算过程中会更改区间内的元素的内容。例如:拷贝,替换,删除等。

​ 何为非质变算法:是指运算过程中不会更改区间内的元素内容,例如:查找,计数,遍历,寻找极值等。

迭代器

迭代器的设计思维-STL 的关键所在,STL 的中心思想在于将容器(container)和算法(algorithms)分开,彼此独立设计,最后再一贴胶着剂将他们撮合在一起。从技术角度来看,容器和算法的泛型化并不困难,c++的 class template 和 functiontemplate 可分别达到目标,如果设计出两这个之间的良好的胶着剂,才是大难题

提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式,迭代器就被发明了出来。

函数符

在STL中主要用来为泛型算法提供策略。

有四种形式:全局函数指针,成员函数指针,仿函数(函数对象),与匿名函数Lambda表达式。

空间配置器

作用:主是用来解决内存碎片化问题的,以及过多的构造无用对象的问题。

容器的空间配置器的作用是把对象的内存开辟和构造过程分开,对象析构和内存释放分离开 。那为什么要把对象的内存开辟和构造(new的两个作用)分开,对象的析构和内存释放(delete的两个作用)分开呢?

1)因为在使用容器的过程中,构造一个容器,我们只想要容器的内存空间,并不需要给我们在内存上构造一堆无用的对象出来(直接用new没有办法避免这个问题);当从容器中删除一个元素的时候,我们需要析构这个被删除对象,但是它占用的容器内存空间是不能释放的,因为容器还要使用;再比如我们需要释放一个容器的时候,需要先把容器里面有效对象元素析构一遍,而不是把容器所有元素都析构一遍(用delete无法避免这个问题),所以在操作容器的过程中,我们需要有选择性的分别去开辟内存,构造对象,析构对象,所以这就是空间配置器存在的意义。

C++ 标准STL里面提供的allocator空间配置器,默认的内存分配和释放就是依赖malloc和free实现的。SGI STL提供了两个版本的空间配置器,一级空间配置器和二级空间配置器。一级空间配置器的内存管理也是通过malloc和free实现的,但是SGI STL的二级空间配置器提供了一个内存池的实现。第二级配置器的设计思想为:

1.如果配置区块超过128 bytes,则移交给第一级配置器处理(空间配置器);

2.如果配置区块小于128 bytes,则采用内存池管理(memory pool)。每次配置一大块内存,则维护对应的自由链表(free-list),下次若再有相同大小的内存需求,就直接从 free-list 中拨出(没有就继续配置内存,具体后面讲述),如果客端释换小额区块,就有配置器回收到 free-list 中。

对于SGI STL的二级空间配置器的内存池实现机制,还是非常重要的,因为这既涉及了空间配置器,又涉及了一个内存池的实现机制,因此大家需要好好的理解它的原理,大家在手写C++空间配置器的过程中,使用过nginx的内存池作为空间配置器的内存管理方案,这又是一个新的积累。

string字符容器库

字符串容器API接口:

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

#include <iostream>

using namespace std;

int main()

{

    string str(15,'g');

    string str10;

    str10.assign(10,'k');

    cout << str << endl;

    string str1(str,5);

    cout << str1 << endl;

    string str2("yaoliang",8);

    cout << str2 << endl;

    const char* s1=str2.data();

    const char* s=str2.c_str();

    cout << s << endl;

    //获取迭代器

    string::iterator it;

    for(it=str2.begin();it!=str2.end();it++){

        cout << *it << "   ";

    }

    cout << endl;

    //获取反向迭代器

    string::reverse_iterator it1;

    for(it1=str2.rbegin();it1!=str2.rend();it1++){

        cout << *it1 << "   ";

    }

    cout << endl;

    cout << str2.size() << endl;

    cout << str2.length() << endl;

    cout << str2.capacity() << endl;//已经开辟好的空间

    cout << str2.max_size() << endl;//最大容量

    //string 容量对象保存字符时,如果是小字符串(小于23个字节),会在栈上开辟空间

    //如果是大对象(大于23个字节),会在堆上开辟空间

    //查看动态开辟空间策略(2倍扩容)

//    string str_test;

//    cout << str_test.capacity() << endl;

//    for(int i=0;i<1024;i++){

//        cout << "string容器对象中的有效元素个数:" << str_test.size() <<

//                ",string容器对象中的容量大小:" << str_test.capacity() <<  endl;//采取2倍扩容机制

//        str_test.push_back('a');

//    }

    cout << str1.insert(0,3,'A') << endl;

    cout << str1 << endl;

    cout << str1.insert(0,"hello") << endl;

    cout << str1.erase(0,6) << endl;

    cout << str2.find("liang") << endl;

    cout << str2.substr(3,5) << endl;

    cout << str2.replace(3,5,"ming");

    return 0;

}

vector容器

vector容器于array数组容器的区别

vector与array无乎是一样的,连续的存储结构,两者的唯一的区别在于在空间上的灵活,数组需要提前指定长度,不量确定了就不能发生改变了,比较死板,不够灵活。比如出现拷贝长度大于了给定的空间,需要再重新定义一个足够空间的大小,然后把旧空间的内容再一个个拷贝到新的空间,非常麻烦。

c++11引入array主要是用来描述一种支持迭代器的C风格数组的,所以数组怎么用,array就怎么用,他定义在栈上,且长度固定不会涉及动态内存的开辟所以没有push_back,pop_back的相关方法,但是Vector这种动态的数组在开发更为常用,他底层对象在堆上,且长度是可变的。

vector容器是动态空间,他随着元素的加入,它的内部机制会自动扩充空间以容纳新的元素。vector的运用对于内存的合理利用与运用的灵活性有很大的帮助。我们再也不必害怕空间不足而一开始就定义一个巨大的数组了。

实现分析:

空间分配策略

1

2

3

4

5

6

7

8

9

10

11

12

13

#include <iostream>

#include <vector>

using namespace std;

int main()

    //动态扩容的策略(没内容不先开辟空间,不同于string)

    vector<int> v;

    for(int i=0;i<100;i++){

        cout << "vector容器对象中的有效元素个数:" << v.size() << "vector容器容量的大小:" << v.capacity() << endl;

        v.push_back(i);

    }

    return 0;

}

迭代器非法化问题及解决

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

#include <iostream>

#include <vector>

using namespace std;

int main()

{

    vector<int> v;

    for(int i=0;i<20;i++){

        v.push_back(rand()%100+1);

    }

    for(int i=0;i<v.size();i++){

        cout << v[i] << " ";

    }

    cout << endl;

    //insert后,迭代器非法化的问题

    for(vector<int>::iterator it=v.begin();it!=v.end();it++){

        if(0==*it%2){

            it=v.insert(it,888);//返回值为迭代器类型

            it++;//更新迭代器

        }

    }

    for(int i=0;i<v.size();i++){

        cout << v[i] << " ";

    }

    cout << endl;

    //erase迭代器非法化的问题

    for(auto it=v.begin();it!=v.end();){

        if(0==*it%2){

            it=v.erase(it);//返回值为容器类型

        }else{

            it++;

        }

    }

    for(int i=0;i<v.size();i++){

        cout << v[i] << " ";

    }

    cout << endl;

    return 0;

}

泛型算法

泛型算法 = 函数模板 + 迭代器范围(注意迭代器的类型) + 函数符(策略使用)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

int main()

{

    vector<int> v;

    for(int i=0;i<20;i++){

        v.push_back(rand()%100+1);

    }

    for(int i=0;i<v.size();i++){

        cout << v[i] << " ";

    }

    cout << endl;

    cout << "----------------使用泛型算法进行遍历-------------" << endl;

    for_each(v.begin(),v.end(),[](int val){cout << val << "-";});

    cout << endl;

    //针对于容器中有迭代器的容器for_each算法,提供了一种变种:枚举for循环

    for(int val:v){//val默认为第0个元素,一次遍历,直到结束为止

        cout << val << "-" ;

    }

    cout << endl;

    return 0;

}

sort:

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

#include <iostream>

#include <vector>

#include <algorithm>

#include <functional>

using namespace std;

template <class T>

T my_greate(T t1,T t2){

    return t1>t2;

}

template <class T>

class my_Greate{

public:

    bool get_my_Greate(T t1,T t2){

        return t1>t2;

    }

};

int main()

{

    vector<int> v;

    for(int i=0;i<20;i++){

        v.push_back(rand()%100+1);

    }

    for(int val:v){

        cout << val << " " ;

    }

    cout << endl;

    cout << "---------------------1---------------------" << endl;

    //sort,ASC//升序

    sort(v.begin(),v.end());

    for(int k:v){

        cout << k << " ";

    }

    cout << endl;

    cout << "----------------------2--------------------" << endl;

    //sort,DESC,使用lambda表达式作为算法策略

    sort(v.begin(),v.end(),[](int val1,int val2){return val1>val2;});//一直判断到最后

//如果val1>val2为真,就把val1排好序,可以把函数符想象成一个抽象(必须这么理解,因为具体排序要看sort实现了),不是算法的具体实现,算法知道意思,就是降序了

    for(int k:v){

        cout << k << " ";

    }

    cout << endl;

    cout << "----------------------3--------------------" << endl;

    //使用函数对象

    sort(v.begin(),v.end(),greater<int>());//这是调用库函数中的函数对象

    for(int k:v){

        cout << k << " ";

    }

    cout << endl;

    cout << "----------------------4--------------------" << endl;

    //定义一个函数指针,函数符来实现

    sort(v.begin(),v.end(),&my_greate<int>);//这个就是全局函数指针,函数符实现

    for(int k:v){

        cout << k << " ";

    }

    cout << endl;

    //定义一个成员函数指针,函数符来实现

    //因为成员函数指针依赖于对象的调用,所以不可以有直接做为策略使用,需要绑定器进行对象绑定才可以

    cout << "----------------------5--------------------" << endl;

    my_Greate<int> m;

    using namespace placeholders;

    sort(v.begin(),v.end(),bind(&my_Greate<int>::get_my_Greate,&m,_1,_2));

    for(int k:v){

        cout << k << " ";

    }

    cout << endl;

    return 0;

}

binary_find:

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

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

int main()

{

    vector<int> v;

    for(int i=0;i<20;i++){

        v.push_back(rand()%100+1);

    }

    for(int val:v){

        cout << val << " " ;

    }

    cout << endl;

    //sort,ASC

    sort(v.begin(),v.end());

    for(int k:v){

        cout << k << " ";

    }

    cout << endl;

    //时间复杂度O(logn)

    bool ok=binary_search(v.begin(),v.end(),70);

    if(ok)

    cout << "find succeed!" << endl;

    else

    cout << "find failure!" << endl;

    return 0;

}

find&&find_if&&bind1st(绑定第一个参数)&&bind2nd(绑定第二个参数)&&bind(新式绑定器):

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

#include <iostream>

#include <vector>

#include <algorithm>

#include <functional>

using namespace std;

int main()

{

    vector<int> v;

    for(int i=0;i<20;i++){

        v.push_back(rand()%100+1);

    }

    for(int val:v){

        cout << val << " " ;

    }

    cout << endl;

    //find对容器中的值没有要求,时间复杂度O(n),顺序遍历一遍,和二分查找不一样,时间复杂度不同

    auto it=find(v.begin(),v.end(),70);

    if(it!=v.end()){

        cout << "find succeed!index:" << it-v.begin() << endl;

    }else{

        cout << "find failure!" << endl;

    }

    //find_if按条件查找,需要把一个2这个值按序插入到序列中

    it=find_if(v.begin(),v.end(),[](int val){return val < 2;});

    if(it!=v.end()){

        it=v.insert(it,2);

        it++;

        cout << "find succeed!" << endl;

    }else{

        cout << "find failure!" << endl;

    }

    for(int val:v){

        cout << val << " " ;

    }

    cout << endl;

    //老式绑定器bind1st,bind2nd

    it=find_if(v.begin(),v.end(),bind1st(greater<int>(),2));

    if(it!=v.end()){

        it=v.insert(it,2);

        it++;

        cout << "find succeed!" << endl;

    }else{

        cout << "find failure!" << endl;

    }

    for(int val:v){

        cout << val << " " ;

    }

    cout << endl;

    //新式绑定器:bind

    using namespace placeholders;

    it=find_if(v.begin(),v.end(),bind(greater<int>(),2,_1));

    if(it!=v.end()){

        it=v.insert(it,2);

        it++;

        cout << "find succeed!" << endl;

    }else{

        cout << "find failure!" << endl;

    }

    for(int val:v){

        cout << val << " " ;

    }

    cout << endl;

    return 0;

}

迭代器与空间配置器

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

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

//实现一个自定的空间配置器

template <class T>

struct Allocate{

    //1.开辟空间

    T* allocate(size_t size){

        T* temp=(T*)malloc(sizeof (T)*size);

        if(nullptr==temp){

            throw bad_alloc();

        }

        return temp;

    }

    //2.构造对象

    void constructor(T* p,const T& obj){

        //定位new

        new (p) T(obj);

    }

    //3.析构对象

    void destructor(T* p){

        p->~T();

    }

    //4.释放空间

    void destroy(T* p){

        free(p);

    }

};

template <class T,class Allocat=Allocate<T>>

class Vector{

private:

    T* _frist;

    T* _last;

    T* _end;

    Allocat _allcater;

public:

    Vector(int size=10){

//        this->_frist=new T[size];

        this->_frist=_allcater.allocate(size);

        this->_last=_frist;

        this->_end=this->_frist+size;

    }

    ~Vector(){

        if(nullptr!=this->_frist){

//            delete [] this->_frist;

            for(T* p=_frist;p!=_last;p++){

                _allcater.destructor(p);

            }

        }

        _allcater.destroy(_frist);

        this->_end=this->_last=this->_frist=nullptr;

    }

    //拷贝构造

    Vector(const Vector& other){

        int size=other._end-other._frist;

//        this->_frist=new T[size];

        this->_frist=_allcater.allocate(size);

        int len=other._end-other._frist;

        memmove(this->_frist,other._frist,sizeof (T)*len);

        this->_last=this->_frist+len;

        this->_end=this->_frist+size;

    }

    //等号运算符重载

    Vector& operator=(const Vector& other){

        if(this==&other){

            return *this;

        }

        int size=other._end-other._frist;

        int len=other._last-other._frist;

        if(nullptr!=this->_frist){

//            delete [] this->_frist;

//            this->_frist=new T[size];

            for(T* p=_frist;p!=_last;p++){

                _allcater.destructor(p);

            }

            _allcater.destroy(_frist);

            this->_frist=_allcater.allcate(size);

        }else{

//            this->_frist=new T[size];

            this->_frist=_allcater.allcate(size);

        }

        memmove(this->_frist,other._frist,sizeof (T)*len);

        this->_last=this->_frist+ len;

        this->_end=this->_frist+size;

        return *this;

    }

    bool full(){

        return this->_last==this->_end;

    }

    bool empty(){

        return  this->_last==this->_frist;

    }

    //2倍扩容

    void expand(){

        int size=this->_end-this->_frist;

//        T* temp=new T[size* 2];

        T* temp=_allcater.allocate(size*2);

        memmove(temp,this->_frist,sizeof (T)*size);

//        delete [] this->_frist;

        for(T* p=_frist;p!=_last;p++){

            _allcater.destructor(p);

        }

        _allcater.destroy(_frist);

        this->_frist=temp;

        this->_last=this->_frist+size;

        this->_end=this->_frist+size*2;

    }

    void push_back(const T& val){

        if(this->full()){

            this->expand();

        }

//        *_last++=val;

        _allcater.constructor(this->_last,val);

        _last++;

    }

    void pop_back(){

        if(this->empty()){

            return;

        }

//        _last--;

        _last--;

        _allcater.destructor(_last);

    }

    int size(){

        return this->_last-this->_frist;

    }

    int capacity(){

        return this->_end-this->_frist;

    }

    T& operator[](int index){

        if(index<0||index>=this->size()){

            throw out_of_range("越界!");

        }

        return this->_frist[index];

    }

    class iterator{

    public:

        //与标准库中的泛型算法匹配类型

        using difference_type=int;

        using value_type= T ;

        using pointer=T*;

        using reference=T&;

        using iterator_category=random_access_iterator_tag;

    private:

        T* ptr;

    public:

        iterator(T* ptr=nullptr){

            this->ptr=ptr;

        }

        //迭代器功能:++运算,!=运算

        iterator& operator++(){

            ++this->ptr;

            return *this;

        }

        iterator& operator++(int){

            this->ptr++;

            return *this;

        }

        iterator& operator--(){

            --this->ptr;

            return *this;

        }

        iterator& operator--(int){

            this->ptr--;

            return *this;

        }

        // !=运算符重载:

        bool operator!=(const iterator& other){

            return this->ptr!=other.ptr;

        }

        bool operator==(const iterator& other){

            return this->ptr==other.ptr;

        }

        T& operator*(){

            return *ptr;

        }

        T* operator->(){

            return ptr;

        }

        iterator& operator+=(int n){

            this->ptr+=n;

            return *this;

        }

        iterator& operator-=(int n){

            this->ptr-=n;

            return *this;

        }

        iterator operator+(int n){

            T* temp=this->ptr+n;

            return iterator(temp);

        }

        iterator operator-(int n){

            T* temp=this->ptr-n;

            return iterator(temp);

        }

        int operator-(const iterator& other){

            return this->ptr-other.ptr;

        }

        bool operator>(const iterator& other){

            return this->ptr - other.ptr > 0;

        }

        bool operator<(const iterator& other){

            return this->ptr - other.ptr < 0;

        }

        bool operator>=(const iterator& other){

            return !(*this < other);

        }

        bool operator<=(const iterator& other){

            return !(*this > other);

        }

        T* get(){

            return ptr;

        }

    };

    iterator begin(){

        return iterator(this->_frist);

    }

    iterator end(){

        return iterator(this->_end);

    }

};

class A{

public:

    A(){

        cout << "A structure" << endl;

    }

    ~A(){

        cout << "A destruct" << endl;

    }

    A(const A&other){

        cout << "A copy" << endl;

    }

};

int main()

{

    Vector<int> v;

    for(int i=0;i<20;i++){

        v.push_back(rand()%100+1);

    }

    for(int i=0;i<v.size();i++){

        cout << v[i] << " " ;

    }

    cout << endl;

    Vector<A> v1;

    Vector<int> v2;

    for(int i=0;i<20;i++){

        v2.push_back(rand()%100+1);

    }

    for(int k:v2){

        cout << k << " " ;

    }

    cout << endl;

    return 0;

}

deque容器

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

#include <iostream>

#include <deque>

#include <algorithm>

using namespace std;

int main()

{

    deque<int> dq;

    for(int i=0;i<20;i++){

        dq.push_front(rand()%100+1);

    }

    for(auto it=dq.begin();it!=dq.end();it++){

        cout << *it << " ";

    }

    cout << endl;

    sort(dq.begin(),dq.end());

    for(int k:dq){

        cout << k << " ";

    }

    cout << endl;

    for(int i=0;i<20;i++){

        cout << dq.back() << " ";

        dq.pop_back();

    }

    cout << endl;

    for(int i=0;i<20;i++){

        cout << dq.front() << " ";

        dq.pop_front();

    }

    cout << endl;

    return 0;

}

容器适配置

容器适配器是没有迭代器的,不能实现泛型算法

使用deque容器实现一个stack

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 <deque>

using namespace std;

template <class T,class Container=deque<T>>

class Stack{

private:

    Container container;

public:

    void push(const T& val){

        container.push_back(val);

    }

    void pop(){

        container.pop_back();

    }

    T& top(){

        return  container.back();

    }

    bool empty(){

        return container.empty();

    }

};

int main()

{

    Stack<int> s;

    for(int i=0;i<10;i++){

        s.push(i);

        cout << i << " ";

    }

    cout << endl;

    while (!s.empty()) {

        cout << s.top() << " ";

        s.pop();

    }

    cout << endl;

    return 0;

}

实现一个单端队列

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 <deque>

using namespace std;

template <class T,class Container=deque<T>>

class SimpleQ{

private:

    Container container;

public:

    void push(const T& val){

        container.push_back(val);

    }

    void pop(){

        container.pop_front();

    }

    T& top(){

        return  container.front();

    }

    bool empty(){

        return container.empty();

    }

};

int main()

{

    SimpleQ<int> q;

    for(int i=0;i<10;i++){

        q.push(i);

        cout << i << " ";

    }

    cout << endl;

    while (!q.empty()) {

        cout << q.top() << " ";

        q.pop();

    }

    cout << endl;

    return 0;

}

优先队列

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 <vector>

#include <algorithm>

using namespace std;

template<class T,class Container=vector<T>,class Compair=less<T>>

class Priority_queue{

private:

    Container container;

    Compair compair;

public:

    Priority_queue(){

        make_heap(container.begin(),container.end(),compair);

    }

    void push(const T& val){

        container.push_back(val);

//        sort(container.begin(),container.end(),compair);//快排,但系统中采取的是大顶堆,原因快排,递归过多

        push_heap(container.begin(),container.end(),compair);

    }

    void pop(){

        pop_heap(container.begin(),container.end(),compair);

        container.pop_back();

    }

    T& top(){

        return  container.front();

//        return container.back();

    }

    bool empty(){

        return container.empty();

    }

};

int main()

{

    Priority_queue<int> pq;

    for(int i=0;i<20;i++){

        pq.push(rand()%100+1);

    }

    while (!pq.empty()) {

        cout << pq.top() << " ";

        pq.pop();

    }

    cout << endl;

    return 0;

}

list容器

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include <iostream>

#include <list>

#include <algorithm>

using namespace std;

int main()

{

    list<int> l;

    for(int i=0;i<20;i++){

        l.push_back(rand()%100+1);

    }

    for(int k:l){

        cout << k << " ";

    }

    cout << endl;

    l.sort();

    for_each(l.begin(),l.end(),[](int val){cout << val << " ";});

    cout << endl;

    l.sort([](int val1,int val2){return val1>val2;});

    for_each(l.begin(),l.end(),[](int val){cout << val << " ";});

    cout << endl;

    return 0;

}

set容器

Set特性是:所有元素都会根据元素的键值自动被排序。(set 与 map都会按照键值来自动排序,只不过set的键与值是一体的相同的)Set的元素不像map那样,可以同时拥有实值与键值,set的元素即是键值又是实值。(你也可以理解为只有一个值,键与值相同)Set不允许两个元素有相同的键值。(即然是自动排序,set与map是不允许有相同的键的存在。这一点与map是共同的。),而multiset是可以存相同键值的元素。(这是set与multiset的唯一区别。)。

所以set容器的迭代器是一个常双向迭代器,只支持什么:++,–,==,!=的操作。

我们可以通过set的迭代器改变set元素的值吗?不行,因为set元素值就是其键值,关系到set元素排序规则,如果任意改变set元素值,会严重破坏set组织结构。换句话说,set的迭代器是一个只读迭代器。

set容器拥有与list某些相同的性质,当对容器中的元素进行插入操作或者删除操作的时候,操作之前所有迭代器,在操作完成之后依

multiset的底层实现是红黑树,红黑树是平衡二叉树的一种。二叉树的相关知识点可以百度一下。

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

#include <iostream>

#include <set>

using namespace std;

class Stu{

private:

    string name;

    int age;

public:

    Stu(string name,int age){

        this->age=age;

        this->name=name;

    }

//    bool operator<(const Stu& other)const{

//        return this->age<other.age;

//    }

    void showInfo()const{

        cout << "姓名:" << name << ",年龄:" << age << endl;

    }

    int getAge(){

        return  this->age;

    }

};

template <class T>

class Myless{

public:

    bool operator()(T t1,T t2)const{

        return t1.getAge()<t2.getAge();

    }

};

int main()

{

    set<int> s;

    for(int i=0;i<20;i++){

        s.insert(rand()%100+1);

    }

    for(int k:s){

        cout << k << " ";

    }

    cout << endl;

    cout << "*********************自定义类型放入set****************" << endl;

    Stu stu1("yaoliang",19);

    Stu stu2("minmin",18);

    Stu stu3("sun",17);

//    set<Stu> s1;//调用库中的<预算符重载函数

//    s1.insert(stu1);

//    s1.insert(stu2);

//    s1.insert(stu3);

//    for(const Stu& stu:s1){

//        stu.showInfo();

//    }

    set<Stu,Myless<Stu>> s1;//自定义比较策略

    s1.insert(stu1);

    s1.insert(stu2);

    s1.insert(stu3);

    for(const Stu& stu:s1){

        stu.showInfo();

    }

    return 0;

}

multiset与set

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

#include <iostream>

#include <set>

using namespace std;

class Stu{

private:

    string name;

    int age;

    int id;

public:

    Stu(string name,int age,int id){

        this->age=age;

        this->name=name;

        this->id=id;

    }

    bool operator<(const Stu& other)const{

        return this->id<other.id;

    }

    void showInfo()const{

        cout << "学号:" << id << ",姓名:" << name << ",年龄:" << age << endl;

    }

    int getAge(){

        return  this->id;

    }

};

int main()

{

    Stu stu1("王大锤",19,1);

    Stu stu2("李二狗",18,2);

    Stu stu3("张三丰",17,3);

    Stu stu4("王五子",17,5);

    Stu stu5("刘四思",20,3);

//    set<Stu> s1;//调用库中的<预算符重载函数

    multiset<Stu> s1;

    s1.insert(stu1);

    s1.insert(stu2);

    s1.insert(stu3);

    s1.insert(stu5);

    s1.insert(stu4);

//    pair<set<Stu>::iterator,bool> p;

//    p=s1.insert(stu4);

//    if(p.second){

//        cout << "王五子入学成功!" << endl;

//    }else{

//        cout << " 王五子入学失败!" << "\n";

//    }

    for(const Stu& stu:s1){

        stu.showInfo();

    }

    return 0;

}

map容器

对组pair构建方式:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#include <iostream>

#include <map>

using namespace std;

int main()

{

    pair<string,int> p1("zhangsan",1001);

    cout << p1.first << "," << p1.second << endl;

    pair<string,int> p2={"lisi",1002};

    cout << p2.first << "," << p2.second << endl;

    pair<string,int> p3;

    p3.first="maliu";

    p3.second=1003;

    cout << p3.first << "," << p3.second << endl;

    pair<string,int> p4=make_pair("wangwu",1004);

    cout << p4.first << "," << p4.second << endl;

    pair<string,int> p5=map<string,int>::value_type("tianqi",1005);

    return 0;

}

Map容器的特性是:所有元素都会根据元素的键的值自动排序。Map所有的元素都是统一的pair对组,同时拥有键值Key实值Value,pair的第一元素被视为键值,第二个元素被视为实值,map不允许两个元素有相同的键。

我们可以通过map迭代器改变map的键值吗?答案是不行,因为map的键值关系到map的元素的排列布局,Map中的键是不可修改的,但是键对应的值是可以修改的。

所以Map的迭代器是一个双向迭代器,只支持++ == !=操作。

Map是可以随时插入或删除键值对的。

Map与multimap的唯一区别是multimap中的键是可以重复的。

Map的底层实现机制是由二叉树中的红黑树进行实现的。

更多推荐