STL是C++语言中一个非常实用的代码库,叫做标准模板库,通常我们使用这个头文件即可导入STL。本文立足与C++,但是python其实也是大同小异。

set

set正如其名,表示的是一个集合,其分为两类,set为数学上的集合,即不含重复元素,multiset为可重集合,即可以包含重复元素我理解这个就是像比于set,insert的时候增加了一维时间戳。两者其内部的实现为一颗红黑树。

set

multiset

定义1

2set 名字;

multiset 名字;

举个例子来说:

1

2

3

4

5set s;

set > ss;

set > ps;

set > vs;

multiset ms;

如果我们需要自己定义类型的话需要自己定义友元函数来重载运算符,与排序是一样的,例如:

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

27using namespace std;

struct {

int x,y;

friend bool operator < (node A,node B){

if(A.x==B.x){

return A.y

}

return A.x

}

};

int main(void){

node a,b;

a.x=1;a.y=2;

b.x=1;b.y=3;

set A;

A.insert(a);

A.insert(b);

auto x=*A.begin();

cout<

x=*(++A.begin());

cout<

}

output:

1 2

1 3

size函数

表示set中的元素个数,返回一个整形变量,时间复杂度为$O(1)$

使用方式:

1printf("%d",(int)s.size());

clear函数

清空一个集合,无返回值

使用方式:

1s.clear()

count函数

返回set中的值为$x$的元素个数,时间按复杂度为$O(log n+an)$

使用方式:

1printf("%dn",s.count(x));

迭代器

set的迭代器是双向的,支持—,++两种操作。若把 ite++ ,则 ite 将会指向“下一个”元素。这里的下一个是指在 key从小到大排序的结果中,排在ite 下一名的元素。同理,若把ite— ,则 ite会指向排在上一个的元素。“++”,“—”操作的复杂度均为$O (1)$。

使用方式:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18using namespace std;

set s;

set::iterator ite;

int main(void){

s.insert(4);

s.insert(5);

ite=s.begin();

cout<

ite++;

cout<

ite--;

cout<

}

output:

4

5

4

begin函数

表示集合的首迭代器,时间复杂度$O(1)$

使用方式:

1cout<

end函数

由于STL都是左闭右开的,那么end返回的位置其实是类似于string中 的位置,所以我们需要—,才能得到最大的元素的迭代器。其实s.end()存储的是集合中的元素个数

使用方式:

1cout<

遍历

我们通过++运算符以及begin函数和end函数来进行遍历整个集合

1

2

3

4

5

6

7

8

9

10

11using namespace std;

set s;

set::iterator ite;

int main(void){

s.insert(4);

s.insert(5);

for(ite=s.begin();ite!=s.end();ite++){

cout<

}

}

insert函数

向set中插入元素,返回值为插入地址的迭代器以及是否插入成功所组成的pair,时间复杂度为$O(log n)$

使用方式:

1s.insert(5);

erase函数

删除,参数可以是元素或者迭代器,返回下一个元素的迭代器,时间复杂度为 O(log n),注意在 multiset 中 s.erase(x)会删除所有值为 $x$ 的元素。所以在multiset中一般是和find函数一起使用的。

使用方式:

1

2s.erase(4);

s.erase(s.find(5));

find函数

在集合中找到第一个元素值等于$x$的迭代器,若不存在的话,返回s.end()。时间复杂度为$O(log n)$

1

2

3if(s.find(3)!=s.end()){

cout<

}

lower_bound和upper_bound函数

用法与$find$类似,但查找的条件略有不同,时间复杂度 $O(log n)$。和vector当中的几乎一样。

$s.lowerbound(x)$表示查找 $>=x$的元素中最小的一个,并返回指向该元素的迭代器。

$s.upper bound(x)$表示查找 $>x$ 的元素中最小的一个,并返回指向该元素的迭代器。

例如:

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

37using namespace std;

set s;

set ::iterator ite;

int main(void){

int a[]={3,4,6,7,9};

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

s.insert(a[i]);

}

ite=s.lower_bound(4);

cout<

ite=s.upper_bound(4);

cout<

ite=s.lower_bound(8);

cout<

ite=s.upper_bound(8);

cout<

ite=s.lower_bound(9);

cout<

ite=s.upper_bound(9);

cout<

ite=s.lower_bound(99);

cout<

ite=s.upper_bound(99);

cout<

}

/*

output:

4

6

9

9

9

5

5

5

*/

map

map表示一个映射,这是非常有用的东西。其主要分为两部分表示的是一个偏序对,左边key为键值,右边value为映射值。

定义1map 名字

举个例子:

1

2

3

4map ii;

map id;

map Ld;

map ,vector > vv;

如果我们需要自定义类型的话,和前面的set一样自己定义友元函数重载运算符即可。

size函数

表示map中存储了多少对映射,时间复杂度$O( 1 )$

使用方式:

1cout<

插入方式

类似数组一样的使用方式,使用[]来进行插入。使劲按复杂度是$O(log n )$

1

2

3

4

5

6

7

8

9

10

11using namespace std;

map ma;

int main(void){

ma["a"]=3;//表示"a"映射为3

ma["b"]=312;//表示"a"映射为312

ma["c"]=43;//表示"a"映射为43

cout<

cout<

cout<

}

迭代器

使用与set一样的方式进行遍历,定义迭代器,但是相应不同的地方就是first与second的用法。其中++,—操作的时间复杂度也是$O(log n)$的。

注意,map中的end迭代器是真的没有用的。我们必须要进行特判

1

2

3

4

5

6

7

8

9

10

11

12#include

using namespace std;

map ma;

map::iterator ite;

int main(void){

ma["a"]=3;//表示"a"映射为3

ma["b"]=312;//表示"a"映射为312

ma["c"]=43;//表示"a"映射为43

for(ite=ma.begin();ite!=ma.end();ite++){

cout<first<second<

}

}

lower_bound和upper_bound函数

map在调用这些函数的时候,是二分查找键值。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17#include

using namespace std;

map ma;

map::iterator ite;

int main(void){

int i,a[]={3,4,6,7,9};

for(i=0;i<5;i++){

ma[a[i]]=i;

}

ite=ma.lower_bound(4);

if(ite!=ma.end()) cout<first<second<

ite=ma.upper_bound(4);

if(ite!=ma.end()) cout<first<second<

}

output:

4 1

6 2

vector

vector是stl中封装好的动态数组实现方式。其有一些特性,我们不妨看看。其中我们需要知道以下两个函数。

size()

capacity()

clear()

前者表示当前的vector存储了多少个数值,时间复杂度为$O(1)$,中者表示的是当前的vector申请类多少内存,后者表示清空当前的vector。

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#include

using namespace std;

vector a;

int main(void){

int i;

for(i=0;i<15;i++){

cout<

a.push_back(i);

}

a.clear();

cout<

}

/*

now size 0 now capacity 0

now size 1 now capacity 1

now size 2 now capacity 2

now size 3 now capacity 4

now size 4 now capacity 4

now size 5 now capacity 8

now size 6 now capacity 8

now size 7 now capacity 8

now size 8 now capacity 8

now size 9 now capacity 16

now size 10 now capacity 16

now size 11 now capacity 16

now size 12 now capacity 16

now size 13 now capacity 16

now size 14 now capacity 16

now size 0 now capacity 16

*/

我们运行上面的代码可以发现vector数组是倍增的,同时clear函数不会改变申请的内存的容量。

sort函数

排序的方式相对也比较简单,具体来说有几种方式。

默认的类型时使用greater方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14#include

using namespace std;

vector a;

int main(void){

int i;

for(i=0;i<15;i++){

a.push_back(rand());

}

//sort(a.begin(),a.end());//增序

sort(a.begin(),a.end(),greater());//降序

for(i=0;i<15;i++){

cout<

}

}

定义cmp函数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20#include

using namespace std;

vector a;

int cmp1(int x,int y){

return x

}

int cmp2(int x,int y){

return x>y;//降序

}

int main(void){

int i;

for(i=0;i<15;i++){

a.push_back(rand());

}

//sort(a.begin(),a.end(),cmp1);

sort(a.begin(),a.end(),cmp2);

for(i=0;i<15;i++){

cout<

python sort函数时间复杂度_STL用法及其时间复杂度分析相关推荐

  1. python sort函数返回值_python中 sort方法 和sorted函数

    sort和sorted在python中常用语列表(或类列表)的排序,但是在python中有所区别. 他们的唯一的共同点恐怕就是都可以对列表排序,区别: 1. sorted是python的内置函数,可以 ...

  2. python sort函数返回值_lambda函数与箭头函数在集合内置函数应用中的对照学习

    Python语言中有一个定义轻量级规则的lambda函数,其语法格式为: Lambda 参数列表:返回值表达式 简单的例子如:定义func=lambda x,y:x+y,则调用func(10,20)的 ...

  3. python sort、sorted 高级用法

    python sort.sorted高级排序技巧 转载原因:学习<机器学习实战>决策树这一章节时,对为了确定叶节点而采用的多数表决法的python程序有些疑惑 故求助度娘,发现了这篇文章, ...

  4. python中函数startswith的用法_Python中的startswith和endswith函数使用实例

    在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数判断文本是否以某个字符开始,endswith()函数判断文本是否以某 ...

  5. python sort 函数和sorted函数的简单区别

    1. sort函数可以对列表进行排序,并改变原列表的内容 2. sored 函数返回一个新的列表

  6. Python sort 函数

    1. sort 函数 函数原型: L.sort(*, key=None, reverse=None) 它把 L 原地排序,也就是使用后并不是返回一个有序的序列副本,而是把当前序列变得有序! 参数说明: ...

  7. python sort函数返回值_如何使用python sort函数?

    不知道大家在做项目时候,有没有遇到这个函数,记得小编第一次看到这个函数,一直纳闷这个函数的使用方法,而后查询了下,今日,小编再一次遇到这个函数,于是,就做了一番整理,内容请看下文. 与sort()函数 ...

  8. python raise函数_Python raise用法

    有读者可能会问,编写代码时能否手动抛出一个异常吗?答案是肯定的,Python 允许程序自行引发异常,使用 raise 语句即可. 异常是一种很"主观"的说法,以下雨为例,假设大家约 ...

  9. python sort函数

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...

最新文章

  1. 【直播】李祖贤:集成学习答疑直播之五 -- 常用集成思路
  2. 玩点深入的:Java 虚拟机内存结构及编码实战
  3. 文本框输入值文字消失常用的两种方法
  4. python的zip方法_python zip()函数使用方法解析
  5. [.Net线程处理系列]专题五:线程同步——事件构造
  6. 你觉得我的这段Java代码还有优化的空间吗?
  7. 计算机网络 —— 总结(面试问题)
  8. [leetcode]求数组的第k个最大值,python快排解法
  9. 教你如何在 Pycharm 中制作自己的爬虫代码模板
  10. HSV颜色空间中颜色(红、黄、绿、 青、蓝、紫、 粉红、 砖红、 品红)对应的灰度范围
  11. 华中师范大学计算机专硕和学硕,学硕?专硕?该如何选
  12. php 卡号算法,验证信用卡卡号代码 Luhn算法
  13. 如何优雅的学习英语?从理解英语的使用思维开始!
  14. php 伪静态规则,常用伪静态规则(for Nginx)
  15. 2013-9百度技术沙龙:Clouda与nodejs
  16. Incapsula 反爬虫
  17. c语言二分答案最小值,C语言程序设计第2版课后习题答案(贾宗璞许合利人民邮电-第2版).doc...
  18. android蓝牙查看电池容量_Android查看电池电量的方法(基于BroadcastReceiver)
  19. 任务活动实现逻辑(领取任务,完成任务)
  20. win快捷键_终于找到了!10个Win+组合快捷键,让你的工作效率提升10倍

热门文章

  1. Spark 2.4重磅发布:优化深度学习框架集成,提供更灵活的流式接收器
  2. Nginx安装及运行服务
  3. DPI 设置过大该如何还原?
  4. LastPass 释出安全更新修复高危漏洞
  5. javascript学习之基本概念
  6. 软件需求阅读笔记之三
  7. pageResponse - 让H5适配移动设备全家(移动端适配)1
  8. WhatFontIs - 字体百科全书,没有不认识的字体
  9. 转 关于 D3D的 SetRenderState 以及AlphaBlend 和AlphaTest
  10. 安装zookeeper