当前位置:网站首页>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)
边栏推荐
- 西安电子科技大学22学年上学期《射频电路基础》试题及答案
- 记录:动态Web项目servlet访问数据库404错误之解决
- First acquaintance with C language (Part 2)
- Answer to "software testing" exercise: Chapter 1
- 167. Sum of two numbers II - input ordered array - Double pointers
- Music playback (toggle & playerprefs)
- System design learning (III) design Amazon's sales rank by category feature
- 初识指针笔记
- How do architects draw system architecture blueprints?
- 六种集合的遍历方式总结(List Set Map Queue Deque Stack)
猜你喜欢
《软件测试》习题答案:第一章
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
抽象类和接口
TYUT太原理工大学2022数据库大题之数据库操作
Dark chain lock (lca+ difference on tree)
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
继承和多态(上)
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
Fundamentals of UD decomposition of KF UD decomposition [1]
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
随机推荐
Heap sort [handwritten small root heap]
IPv6 experiment
Alibaba cloud microservices (IV) service mesh overview and instance istio
One article to get UDP and TCP high-frequency interview questions!
西安电子科技大学22学年上学期《信号与系统》试题及答案
最新坦克大战2022-全程开发笔记-3
Tyut Taiyuan University of technology 2022 introduction to software engineering
几道高频的JVM面试题
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
[rtklib 2.4.3 B34] version update introduction I
继承和多态(上)
MPLS experiment
4.30 dynamic memory allocation notes
Record: solution of 404 error of servlet accessing database in dynamic web project
Solution: warning:tensorflow:gradients do not exist for variables ['deny_1/kernel:0', 'deny_1/bias:0',
2-year experience summary, tell you how to do a good job in project management
Tyut outline of 2022 database examination of Taiyuan University of Technology
Interview Essentials: talk about the various implementations of distributed locks!
TYUT太原理工大学2022数据库考试题型大纲
TYUT太原理工大学2022数据库大题之概念模型设计