set基础
//set类似于不存放 重复元素的 vector
//set的功能是去重并排序
//set的元素既是key值,又是value值
//multiset特性及用法和set完全相同, 唯一的区别是 multiset允许重复.
//multiset和set的底层实现都是红黑树
eg:
set<int>s;
set<int>::const_iterator it = s.begin();
count(key);
#include<iostream>
#include<set>
using namespace std;
int main() {
set<int>s;
int n, t;
cout << "大哥,这次您想要输入多少个元素啊?" << endl;
cin >> n;
cout<<"大哥要存放"<<n<<"个" << endl;
for (int i = 0; i < n;i++) {
cin >> t;
s.insert(t);
}
std::set<int>::iterator it = s.begin();
for (; it != s.end();it++) {
cout << *it << " ";
}
cout << "\n方式二打印:" << endl;
for (auto j:s) {
cout << j<<" ";
}
cout << "\n寻找元素5:" << endl;
auto j = s.find(5);
if (j != s.end()) {
cout << "5找到了" << endl;
cout << "*j的值:" << *j << endl;
}
cout<<"\n删除erase的用法:" << endl;
if (j != s.end()) s.erase(5);
for (auto j : s) {
cout << j << " ";
}
cout << "\n利用迭代器删除元素" << endl;
for (; it != s.end();) {
if (*it == 2)it = s.erase(it);
else it++;
}
for (auto j : s) {
cout << j << " ";
}
cout << "\n如果你想删除某一个数(比如:1)的后面一个数,那该怎么办?" << endl;
auto j2 = s.find(1);
j2++;
if (j2!= s.end()) {
j2 = s.erase(j2);
}
for (auto j : s) {
cout << j << " ";
}
cout << endl;
system("pause");
return 0;
}
set进阶,lower_bound、upper_bound、equal_range的用法
lower_bound(keyElem);
upper_bound(keyElem);
equal_range(keyElem);
#include<iostream>
#include<set>
using namespace std;
int main(){
set<int> s;
s.insert(30);
s.insert(40);
s.insert(50);
s.insert(60);
s.insert(10);
set<int>::const_iterator lower_ret;
lower_ret = s.lower_bound(30);
if(lower_ret == s.end()){
cout<<"没找到30的下限"<<endl;
}else{
cout<<"找到了30的下限"<<*lower_ret<<endl;
}
set<int>::const_iterator up_ret;
up_ret = s.upper_bound(30);
if(up_ret == s.end()){
cout<<"没找到30的上限"<<endl;
}else{
cout<<"找到了30的上限"<<*up_ret<<endl;
}
pair<
set<int>::const_iterator,
set<int>::const_iterator
>pair_ret;
pair_ret = s.equal_range(30);
if(pair_ret.first == s.end()){
cout<<"pair_ret没找到30的下限 "<<endl;
}else{
cout<<"pair_ret找到了30的下限 : "<<*pair_ret.first<<endl;
}
if(pair_ret.second == s.end()){
cout<<"pair_ret没找到30的上限 "<<endl;
}else{
cout<<"pair_ret找到了30的上限 : "<<*pair_ret.second<<endl;
}
return 0;
}
运行结果:
找到了30的下限30
找到了30的上限40
pair_ret找到了30的下限 : 30
pair_ret找到了30的上限 : 40
set进阶----》用仿函数修改set的排序规则
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
class Mysort
{
public:
bool operator()(int val1, int val2){
return val1>val2;
}
};
int main()
{
set<int,Mysort> s;
s.insert(30);
s.insert(10);
s.insert(20);
s.insert(50);
s.insert(40);
for_each(s.begin(), s.end(), [](int val)
{ cout << val << " "; });
return 0;
}
set进阶--------》针对set如何存放自定义类型
#include <iostream>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
class Person
{
public:
string name;
int age;
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator<(const Person &ob) const
{
return this->age < ob.age;
}
};
class Mysort
{
public:
bool operator()(const Person &ob1,const Person &ob2){
return ob1.age>ob2.age;
}
};
int main()
{
set<Person, Mysort> s;
s.insert(Person("德玛西亚", 18));
s.insert(Person("德邦总管", 19));
s.insert(Person("小法世界", 20));
s.insert(Person("小炮射手", 29));
for_each(s.begin(), s.end(), [](Person val)
{ cout << "name:" << val.name << ",age:" << val.age << endl; });
return 0;
}
运行结果:
name:小炮射手,age:29
name:小法世界,age:20
name:德邦总管,age:19
name:德玛西亚,age:18