目录
1.set容器的构造和赋值
2.set的大小和交换
3.set的插入和删除
4.set的查找和统计
5.set和mutiset区别
6.pair对组的创建
7.set排序
1.set的内置类型指定排序规则
2.set的自定义数据类型指定排序
1.set容器的构造和赋值
#include<bits/stdc++.h>
using namespace std;void print(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{set<int> s;s.insert(7);s.insert(3);s.insert(9);s.insert(3);s.insert(6);print(s);set<int>s1(s);print(s1);set<int>s2;s2 = s1;print(s2);return 0;
}
2.set的大小和交换
#include<bits/stdc++.h>
using namespace std;void print(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{set<int> s;s.insert(7);s.insert(3);s.insert(9);s.insert(3);s.insert(6);print(s);cout << s.empty() << endl;cout << s.size() << endl;set<int>s1;s1.swap(s);print(s1);return 0;
}
3.set的插入和删除
#include<bits/stdc++.h>
using namespace std;void print(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{set<int> s;s.insert(7);s.insert(3);s.insert(9);s.insert(3);s.insert(6);print(s);s.erase(s.begin());//删除删的是排序后那个下标所在的位置的值print(s);s.erase(6);//也可以直接删除你想删的那个值print(s);s.clear();print(s);return 0;
}
4.set的查找和统计
#include<bits/stdc++.h>
using namespace std;void print(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{set<int> s;s.insert(7);s.insert(3);s.insert(9);s.insert(3);s.insert(6);print(s);set<int>::iterator pos = s.find(6);if(pos != s.end()) cout << "找到了" << *pos << endl;cout << s.count(9) << endl;return 0;
}
5.set和mutiset区别
#include<bits/stdc++.h>
using namespace std;void print(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{set<int> s;pair<set<int>::iterator,bool> ret = s.insert(7);cout << ret.second << endl;ret = s.insert(7);cout << ret.second << endl;//说明第一次插入成功,而第二次插入失败//而mutiset里却可以插入重复的值return 0;
}
6.pair对组的创建
#include<bits/stdc++.h>
using namespace std;void print(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}int main()
{set<int> s;pair<string,int>p1("avent",100);cout << p1.first << " " << p1.second << endl;pair<string,int>p2 = make_pair("misha",12);cout << p2.first << " " <<p2.second;return 0;
}
7.set排序
1.set的内置类型指定排序规则
#include<bits/stdc++.h>
using namespace std;void print(set<int> &s)
{for(set<int>::iterator it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;
}class cmp
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};int main()
{set<int> s;s.insert(10);s.insert(80);s.insert(200);s.insert(90);s.insert(180);print(s);set<int,cmp> s1;s1.insert(10);s1.insert(80);s1.insert(200);s1.insert(90);s1.insert(180);for(set<int,cmp>::iterator it = s1.begin(); it != s1.end(); it++){cout << *it << " ";}cout << endl;return 0;
}
2.set的自定义数据类型指定排序
#include<bits/stdc++.h>
using namespace std;class person
{
public:person(int a,int b){ma = a;mb = b;}int ma;int mb;
};class cmp
{
public:bool operator()(const person &p1,const person &p2){if(p1.ma == p2.ma) return p1.mb < p2.mb;else return p1.ma < p2.ma;}
};int main()
{set<person,cmp> s;person p1(10,100);person p2(20,200);person p3(10,200);s.insert(p1);s.insert(p2);s.insert(p3);for(set<person,cmp>::iterator it = s.begin(); it != s.end(); it++){cout << (*it).ma << " " << (*it).mb << " " << endl;}return 0;
}