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/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值