当前位置:网站首页>Set container
Set container
2022-07-06 13:21:00 【Rivers overflow】
#include<set>
using std::set;
set Containers that can be automatically ordered internally and do not contain duplicate elements
1.set The definition of
set<typename> name;
Precautions are the same vector
2.set Access to elements within a container
differ vector,set Inner elements can only be accessed through iterators , And it does not support the behavior of iterators adding integers , Such as (it+i)i Writing as an integer is not allowed
The traversal method can only be
for(set<int>::iterator it=si.begin();it!=si.end();it++)
cout<<*it<<endl;Be careful : Iterator not supported it<si.end() How to write it
3.set Common function analysis
(1)insert()
insert(x) take x Insert set In the container , And automatically incremental sorting and de duplication , The time complexity is O(logn),n by set The number of elements in
(2)find()
find(value) The return corresponding value is value The iterator , The time complexity is O(logn),n by set The number of elements in
set<int>::iterator it=set.find(3);// The return corresponding value is 3 The iterator (3)erase()
①si.erase(it),it Iterator for the element to be deleted . The time complexity is O(1), Can be combined with find() Use
si.erase(si.find(200));// The deletion value is 200 The elements of ,erase by O(1),find by O(logn)②si.erase(value),value For a value . Delete value value The elements of , Time complexity O(logn)
si.erase(200);// The deletion value is 200 The elements of , Time complexity O(logn)③si.erase(first,last), Delete [first,last) Elements of left closed and right open intervals , Time complexity O(last-first)
set<int>::iterator it=si.find(30);
si.erase(it,si.end());(4)size()
Time complexity O(1)
(5)clear()
Time complexity O(n)
4. Common use
Automatic de duplication and ascending sort
If you need to use the case without weight removal , Use multiset
If you need to use the case of no sorting , Use unordered_set( Much faster than set)
边栏推荐
- 初识C语言(上)
- Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
- Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
- 系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
- One article to get UDP and TCP high-frequency interview questions!
- Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
- TYUT太原理工大学2022数据库之关系代数小题
- Small exercise of library management system
- Common method signatures and meanings of Iterable, collection and list
- 六种集合的遍历方式总结(List Set Map Queue Deque Stack)
猜你喜欢

System design learning (I) design pastebin com (or Bit.ly)

凡人修仙学指针-2

TYUT太原理工大学2022数据库题库选择题总结

121 distributed interview questions and answers

继承和多态(下)

阿里云微服务(三)Sentinel开源流控熔断降级组件

几道高频的JVM面试题

View UI plus released version 1.2.0 and added image, skeleton and typography components

9.指针(上)

TYUT太原理工大学2022数据库大题之概念模型设计
随机推荐
分支语句和循环语句
Heap sort [handwritten small root heap]
View UI plus released version 1.3.1 to enhance the experience of typescript
View UI plus released version 1.2.0 and added image, skeleton and typography components
Employment of cashier [differential constraint]
学编程的八大电脑操作,总有一款你不会
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
错误: 找不到符号
西安电子科技大学22学年上学期《射频电路基础》试题及答案
架构师怎样绘制系统架构蓝图?
【快趁你舍友打游戏,来看道题吧】
Database operation of tyut Taiyuan University of technology 2022 database
[while your roommate plays games, let's see a problem]
String class
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
How to ensure data consistency between MySQL and redis?
4.30动态内存分配笔记
2-year experience summary, tell you how to do a good job in project management
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
Introduction pointer notes