当前位置:网站首页>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)
边栏推荐
- 10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
- View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
- Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
- Application architecture of large live broadcast platform
- Alibaba cloud microservices (IV) service mesh overview and instance istio
- Dark chain lock (lca+ difference on tree)
- Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
- 《软件测试》习题答案:第一章
- TYUT太原理工大学2022数据库大题之分解关系模式
- First acquaintance with C language (Part 1)
猜你喜欢

Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited

Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing

Design a key value cache to save the results of the most recent Web server queries

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

TYUT太原理工大学2022数据库大题之概念模型设计

Cloud native trend in 2022

How to ensure data consistency between MySQL and redis?

Music playback (toggle & playerprefs)

Conceptual model design of the 2022 database of tyut Taiyuan University of Technology

继承和多态(下)
随机推荐
View UI plus released version 1.2.0 and added image, skeleton and typography components
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
Atomic and nonatomic
面试必备:聊聊分布式锁的多种实现!
Quickly generate illustrations
ROS machine voice
All in one 1405: sum and product of prime numbers
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
Record: the solution of MySQL denial of access when CMD starts for the first time
Edit distance (multi-source BFS)
2年经验总结,告诉你如何做好项目管理
Music playback (toggle & playerprefs)
167. Sum of two numbers II - input ordered array - Double pointers
9.指针(上)
View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
Interview Essentials: talk about the various implementations of distributed locks!
Smart classroom solution and mobile teaching concept description
Introduction pointer notes
TYUT太原理工大学2022“mao gai”必背
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client