【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
using namespace std;
int main() {
int a[]= {7,2,6,5,1,9,8};
set myset(a,a+5); //a[0]~a[4]:7,2,6,5,1
cout<<"myset contains:";
set::iterator it;
for(it=myset.begin(); it!=myset.end(); it++)
cout<<" "<<*it;
cout<
☑ 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
using namespace std;
int main() {
int a[]= {7,2,6,5,1,9,8};
set myset;
myset.insert(a+1,a+6); //a[1]~a[5]:2,6,5,1,9
cout<<"myset contains:";
set::iterator it;
for(it=myset.begin(); it!=myset.end(); it++)
cout<<" "<<*it;
cout<
☑ 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
using namespace std;
int main () {
set myset;
for(int i=1;i<=5;i++) myset.insert(i);
myset.erase(myset.find(4));
cout<<"myset contains:";
set::iterator it;
for(it=myset.begin(); it!=myset.end(); it++)
cout<<" "<<*it;
cout<
☑ 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
using namespace std;
int main() {
int a[]= {7,2,6,5,1,9,8};
set 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::iterator first,last;
first=myset.find(6);
last=myset.find(9);
myset.erase(first,last); //2,9
set::iterator it;
for(it=myset.begin(); it!=myset.end(); it++)
cout<<" "<<*it;
cout<
☑ 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
using namespace std;
int main () {
set myset;
for(int i=1; i<=9; i++) myset.insert(i);
set::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::iterator it;
for(it=myset.begin(); it!=myset.end(); it++)
cout<<" "<<*it;
cout<
【参考文献】
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/