IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> STL set 常用函数解析 -> 正文阅读

[C++知识库]STL set 常用函数解析

【STL set 官方文档】
https://cplusplus.com/reference/set/
https://cplusplus.com/reference/set/set/
STL set 是一种关联式容器。其内的元素,默认
自动去重后按增序重排
STL set 官方文档 https://cplusplus.com/reference/set/set/ 将其描述为 “
Sets are containers that store unique elements following a specific ascending order. Sets are typically implemented as binary search trees.”。

【STL set 常用函数】
? set::begin/end 官方文档链接及示例代码
https://cplusplus.com/reference/set/set/begin/
https://cplusplus.com/reference/set/set/end/

set::begin ?Returns an iterator referring to the first element in the set container.
set::end??Returns an iterator referring to the past-the-end element in the set container.?(past-the-end:返回集合最后一个元素的下一个位置)

//set::begin/end
#include <bits/stdc++.h>
using namespace std;

int main() {
	int a[]= {7,2,6,5,1,9,8};
	set<int> myset(a,a+5); //a[0]~a[4]:7,2,6,5,1

	cout<<"myset contains:";
	set<int>::iterator it;
	for(it=myset.begin(); it!=myset.end(); it++)
		cout<<" "<<*it;
	cout<<endl;

	return 0;
}


/*
Output:
myset contains: 1 2 5 6 7
*/

??set::insert?官方文档链接及示例代码
https://cplusplus.com/reference/set/set/insert/
Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted. Because elements in a set are unique, the insertion operation checks whether each inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).
Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted in the container. Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

//set::insert
#include <bits/stdc++.h>
using namespace std;

int main() {
	int a[]= {7,2,6,5,1,9,8};
	set<int> myset;
	myset.insert(a+1,a+6); //a[1]~a[5]:2,6,5,1,9

	cout<<"myset contains:";
	set<int>::iterator it;
	for(it=myset.begin(); it!=myset.end(); it++)
		cout<<" "<<*it;
	cout<<endl;

	return 0;
}


/*
Output:
myset contains: 1 2 5 6 9
*/

? set::find 官方文档链接及示例代码
https://cplusplus.com/reference/set/set/find/
Searches the container for an element equivalent to?val?and returns an iterator to it if found, otherwise it returns an iterator to set::end.

//set::find
#include <bits/stdc++.h>
using namespace std;

int main () {
	set<int> myset;
	for(int i=1;i<=5;i++) myset.insert(i);
	myset.erase(myset.find(4));

	cout<<"myset contains:";
	set<int>::iterator it;
	for(it=myset.begin(); it!=myset.end(); it++)
		cout<<" "<<*it;
	cout<<endl;

	return 0;
}


/*
Output:
myset contains: 1 2 3 5
*/

??set::erase?官方文档链接及示例代码
https://cplusplus.com/reference/set/set/erase/
Removes from the set container either a single element or a range of elements ([first,last)).

//set::erase
#include <bits/stdc++.h>
using namespace std;

int main() {
	int a[]= {7,2,6,5,1,9,8};
	set<int> myset;
	myset.insert(a,a+6); //a[0]~a[5]:7,2,6,5,1,9

	cout<<"myset contains:";
	myset.erase(myset.begin()); //2,5,6,7,9
	myset.erase(5); //2,6,7,9
	set<int>::iterator first,last;
	first=myset.find(6);
	last=myset.find(9);
	myset.erase(first,last); //2,9

	set<int>::iterator it;
	for(it=myset.begin(); it!=myset.end(); it++)
		cout<<" "<<*it;
	cout<<endl;

	return 0;
}


/*
Output:
myset contains: 2 9
*/

??set::lower_bound/upper_bound 官方文档链接及示例代码
https://cplusplus.com/reference/set/set/lower_bound/
https://cplusplus.com/reference/set/set/upper_bound/
set::lower_bound → //Returns an iterator to the the first element in the container which is not considered to go before val(i.e., >=val), or set::end if all elements are considered to go before val.(默认增序,故 first element 为从左到右遍历集合时满足>=val条件的第一个元素的位置) set::upper_bound → Returns an iterator to the the first element in the container which is considered to go after val(i.e., >val), or set::end if no elements are considered to go after val. (默认增序,故 first element 为从左到右遍历集合时满足>val条件的第一个元素的位置)

//set::lower_bound/upper_bound
#include <bits/stdc++.h>
using namespace std;

int main () {
	set<int> myset;
	for(int i=1; i<=9; i++) myset.insert(i);

	set<int>::iterator itlow,itup;
	itlow=myset.lower_bound(3); // >=3, including 3
	itup=myset.upper_bound(6); // >6 →from 7, not including 6
	myset.erase(itlow,itup); // delete [3,7) →keep 1 2 7 8 9

	cout<<"myset contains:";
	set<int>::iterator it;
	for(it=myset.begin(); it!=myset.end(); it++)
		cout<<" "<<*it;
	cout<<endl;

	return 0;
}


/*
Output:
myset contains: 1 2 7 8 9
*/


【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/127023239
https://cplusplus.com/reference/set/
https://cplusplus.com/reference/set/set/
https://cplusplus.com/reference/set/set/begin/
https://cplusplus.com/reference/set/set/end/
https://cplusplus.com/reference/set/set/insert/
https://cplusplus.com/reference/set/set/find/
https://cplusplus.com/reference/set/set/erase/
https://cplusplus.com/reference/set/set/lower_bound/
https://cplusplus.com/reference/set/set/upper_bound/

?

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-24 20:38:18  更:2022-09-24 20:41:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 16:01:23-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码