Detailed set of common usage
set as a collection of translation, it is a internal self-ordering and no repeating elements of the container
head File
#include<set>
using namespace std;
Defined set of
Single set
set<typename> name;
set<int> name;
set<double> name;
set<char> name;
set<node> name; // node是结构体的类型
set array
set<typename> ArrayName[arraySize];
set<int> a[1000];
Set inside a container element access
Can only be accessed by the iterator
set<typename>::iterator it;
In addition to vector and string of STL containers can not be used * (it + i) access methods , we can only enumerate:
#include<cstdio>
#include<set>
using namespace std;
int main(){
set<int> st;
st.insert(3);
st.insert(5);
st.insert(2);
st.insert(3);
// 注意:不支持it < st.end()写法
for(set<int>::iterator it = st.begin(); it != st.end(); it++){
printf("%d ", *it);
}
return 0;
}
2 3 5
The results can be found by the elements in the set automatically in ascending order, and automatically removing repetitive elements
set of commonly used functions
insert( )
insert(x)
X set may be inserted into the container, and automatically in ascending order and de-emphasis, time complexity isO(logN)
, N is the number of elements in the set
find( )
find(value)
Returns the value of the corresponding set value of the iterator, time complexity isO(logN)
, N is the number of elements in the set
#include<cstdio>
#include<set>
using namespace std;
int main(){
set<int> st;
for(int i = 1; i <=10; i += 2){
st.insert(i);
}
for(set<int>::iterator it = st.begin(); it != st.end(); it++){
printf("%d ", *it);
}
set<int>::iterator it = st.find(7);
printf("\n%d\n", *it);
return 0;
}
1 3 5 7 9
7
erase( )
Delete individual items
st.erase(it)
, It is required to remove elements of the iterator, time complexityO(1)
, it can be combinedfind()
using
#include<cstdio>
#include<set>
using namespace std;
int main(){
set<int> st;
st.insert(100);
st.insert(200);
st.insert(100);
st.insert(300);
st.erase(st.find(100)); // 利用find找到100,然后用erase删除
st.erase(st.find(200));
for(set<int>::iterator it = st.begin(); it != st.end(); it++)
printf("%d", *it);
return 0;
}
st.erase(value)
,value
To remove the element of a desired value, the time complexityO(logN)
,N
asset
the number of elements
#include<cstdio>
#include<set>
using namespace std;
int main(){
set<int> st;
st.insert(100);
st.insert(200);
st.insert(100);
st.insert(300);
st.erase(100); // 删除set中值为100的元素
for(set<int>::iterator it = st.begin(); it != st.end(); it++)
printf("%d ", *it);
return 0;
}
Delete elements within a range
st.erase(first, last)
You can delete all the elements within a range, delete interval is the [first, last)
time complexity isO(last - first)
#include<cstdio>
#include<set>
using namespace std;
int main(){
set<int> st;
st.insert(100);
st.insert(200);
st.insert(100);
st.insert(300);
set<int>::iterator it = st.find(200);
st.erase(it, st.end()); // 删除元素200至set末尾之间的元素,即200,300
for(set<int>::iterator it = st.begin(); it != st.end(); it++)
printf("%d ", *it);
return 0;
}
size( )
size()
Used to obtain the number of elements within the set, the time complexity isO(1)
#include<cstdio>
#include<set>
using namespace std;
int main(){
set<int> st;
st.insert(100);
st.insert(200);
st.insert(100);
st.insert(300);
printf("%d", st.size());
return 0;
}
clear( )
clear()
To clear all of the elements in the set, the complexity ofO(N)
#include<cstdio>
#include<set>
using namespace std;
int main(){
set<int> st;
st.insert(100);
st.insert(200);
st.insert(100);
st.insert(300);
st.clear();
printf("%d", st.size());
return 0;
}
Common uses set of
- To automatically re-sorted in ascending
Write by Gqq