恭喜主教大人完成了自己的目标!!! (´▽`)
set 容器
set容器基本概念
简介:
-
所有元素都会在插入时所有元素都会在插入时自动被排序(自动去重/可重复插不报错但是去重了,默认从小到大排)
本质:
-
set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别
-
set不允许容器中有重复的元素
-
multiset允许容器中有重复的元素
set容器的使用(表格)
函数 | 意义 | 使用 |
---|---|---|
insert() | set容器唯一的插入数据方式 | a.insert(c); |
size() | 判断容器中元素的数目 | cout << "d容器中有元素" << d.size() << "个" << endl; |
empty() | 判断容器是否为空 | if (d.empty()) { cout << "d容器为空" << endl; } else { cout << "容器不为空" << endl; cout << "d容器中有元素" << d.size() << "个" << endl; } |
swap(st) | 交换两个集合容器 | a.swap(d); |
insert() | 在容器中插入元素 | a.insert(c); |
clear() | 清除所有元素 | a.clear(); |
erase(pos) | 删除pos迭代器所指的元素,返回下一个元素的迭代器(删小的) | a.erase(a.begin()); |
erase(beg,end) | 删除区间(beg(),end())的所有元素,返回下一个元素的迭代器 | a.erase(a.begin(), a.end()); |
erase(elem) | 删除容器中值为elem的元素 | a.erase(b); |
find(key) | 查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end(); | if (v.find(g) == v.end()) { cout << "无该元素" << endl; } else { cout << "存在该元素" << endl; } set<int>::iterator a = v.find(g); |
count(key) | 统计key的元素个数(0或者1) | cout << "v容器中的元素数" << v.count(x) << endl; |
set构造和赋值
功能描述:创建set容器以及赋值
set赋值
#include<iostream>
#include<set>
using namespace std;
void printset(const set<int>&a)
{
for (set<int>::iterator it = a.begin(); it != a.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void insert(set<int>& a)
{
int b;
cout << "输入要插入树中的元素数" << endl;
cin >> b;
for (int i = 0; i < b; i++)
{
int c;
cout << "输入要存入的数" << endl;
cin >> c;
a.insert(c);
}
}
int main()
{
set<int>a;
insert(a);
printset(a);
set<int>b;
b = a;
printset(b);
set<int>c(b);
printset(c);
system("pause");
return 0;
}
set大小和交换
功能描述:
-
统计set容器大小以及交换set容器
#include<iostream>
#include<set>
using namespace std;
void printset(const set<int>&a)
{
for (set<int>::iterator it = a.begin(); it != a.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void insert(set<int>& a)
{
int b;
cout << "输入要插入树中的元素数" << endl;
cin >> b;
for (int i = 0; i < b; i++)
{
int c;
cout << "输入要存入的数" << endl;
cin >> c;
a.insert(c);
}
}
int main()
{
set<int>a;
insert(a);
printset(a);
set<int>b;
b = a;
printset(b);
set<int>c(b);
printset(c);
set<int>d;
insert(d);
printset(d);
if (a.empty())
{
cout << "a容器为空" << endl;
}
else
{
cout << "容器不为空" << endl;
cout << "a容器中有元素" << a.size() << "个" << endl;
}
if (d.empty())
{
cout << "d容器为空" << endl;
}
else
{
cout << "容器不为空" << endl;
cout << "d容器中有元素" << d.size() << "个" << endl;
}
a.swap(d);
printset(a);
printset(d);
system("pause");
return 0;
}
set插入和删除
功能描述:
-
set容器进行插入数据和删除数据
#include<iostream>
#include<set>
using namespace std;
void printset(const set<int>&a)
{
for (set<int>::iterator it = a.begin(); it != a.end(); it++)
{
cout << *it << " " ;
}
cout << endl;
}
void insert(set<int>& a)
{
int b;
cout << "输入要插入树中的元素数" << endl;
cin >> b;
for (int i = 0; i < b; i++)
{
int c;
cout << "输入要存入的数" << endl;
cin >> c;
a.insert(c);
}
}
void deleteset(set<int>& a)
{
int b;
cout << "输入要删除树中的元素数" << endl;
cin >> b;
for (int i = 0; i < b; i++)
{
a.erase(a.begin());
}
}
void deleteset2(set<int>& a)
{
int b;
cout << "输入要删除树中的元素" << endl;
cin >> b;
a.erase(b);
}
void deleteset3(set<int>&a)
{
a.erase(a.begin(), a.end());
}
int main()
{
set<int>a;
insert(a);
printset(a);
set<int>b;
b = a;
printset(b);
set<int>c(b);
printset(c);
set<int>d;
insert(d);
printset(d);
if (a.empty())
{
cout << "a容器为空" << endl;
}
else
{
cout << "容器不为空" << endl;
cout << "a容器中有元素" << a.size() << "个" << endl;
}
if (d.empty())
{
cout << "d容器为空" << endl;
}
else
{
cout << "容器不为空" << endl;
cout << "d容器中有元素" << d.size() << "个" << endl;
}
a.swap(d);
printset(a);
printset(d);
a.clear();
printset(a);
deleteset(d);
printset(d);
deleteset2(d);
printset(d);
deleteset3(d);
printset(d);
system("pause");
return 0;
}
set容器查找和统计
功能描述:
-
对set容器进行查找数据以及统计数据
函数原型:
#include<iostream>
#include<set>
using namespace std;
void printset(const set<int>& a)
{
for (set<int>::iterator it = a.begin(); it != a.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void insert(set<int>& a)
{
int b;
cout << "输入要插入树中的元素数" << endl;
cin >> b;
for (int i = 0; i < b; i++)
{
int c;
cout << "输入要存入的数" << endl;
cin >> c;
a.insert(c);
}
}
int main()
{
set<int>v;
insert(v);
printset(v);
int g;
cout << "输入要查找的元素" << endl;
cin >> g;
if (v.find(g) == v.end())
{
cout << "无该元素" << endl;
}
else
{
set<int>::iterator a = v.find(g);
cout << "存在该元素" << endl;
}
int x;
cout << "输入要查找的元素数" << endl;
cin >> x;
cout << "v容器中的元素数" << v.count(x) << endl;
system("pause");
return 0;
}
set 和multiset区别
区别:
-
set不可以插入重复数据,而multiset可以
-
set插入数据的同时会返回插入结果,表示插入是否成功
-
multiset不会检测数据,因此可以插入重复数据
pair<set<int>::iterator,bool> a=s.insert(10);
if(a.second)
{
cout<<"第一次插入成功"<<endl;
}else
{
cout<<"第一次插入失败"<<endl;
}
a=s.insert(10);
if(a.second)
{
cout<<"第二次插入成功"<<endl;
}else
{
cout<<"第二次插入失败"<<endl;
}
multiset<int>b;
b.insert(10);
if(b.second)
{
cout<<"第一次插入成功"<<endl;
}else
{
cout<<"第一次插入失败"<<endl;
}
b.insert(10);
if(b.second)
{
cout<<"第二次插入成功"<<endl;
}else
{
cout<<"第二次插入失败"<<endl;
}
for(multiset<int>::iterator it=b.begin;it!=b.end();it++)
{
cout<<*it<<endl;
}
set容器排序
-
set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术:
-
利用仿函数,可以改变排序规则(仿函数重载函数调用的())
1.内置类型指定排序规则2.自定义数据类型排序规则
#include<iostream>
#include<set>
using namespace std;
class mycompare
{
public:
bool operator()(模板列表中数据类型 a,模板列表中数据类型 b)const
{
return a>b;
}
};
void printset(set<int>a)
{
for(set<int>::const_iterator it=a.begin();it!=a.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
void pushset(set<int>&a)
{
int b;
cout<<"输入要存入的数量"<<endl;
cin>>b;
for(int i=0;i<b;i++)
{
int c;
cout<<"输入要存入的数据"<<endl;
cin>>c;
a.insert(c);
}
}
int main()
{
set<int>a;
pushset(a);
printset(a);
set<int,mycompare>b;
pushset(b);
//printset(b);这个行不通了
for(set<int,mypare>::iterator it=b.begin();it!=b.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
system("pause");
return 0;
}
-
自定义数据类型通常指定排序规则
失败版本
#include<iostream>
#include<set>
#include<string>
using namespace std;
class juese
{
public:
string name;
string yuanshu;
//int mingzuo;
string wuqi;
int zhanlijieji;
juese()
{
cout << "输入角色姓名" << endl;
cin >> name;
cout << "输入角色属性" << endl;
cin >> yuanshu;
//cout << "输入角色核心命坐" << endl;
//cin >> mingzuo;
cout << "输入角色武器" << endl;
cin >> wuqi;
cout << "输入角色强度阶级" << endl;
cin >> zhanlijieji;
}
void printset2() //遍历内置的
{
cout << "角色" << name << "是" << yuanshu << "属性角色" <<"使用"<<wuqi<< "武器" << endl;
}
/*bool operator()(juese a, juese b)
{
return a.zhanlijieji>b.zhanlijieji;
}*/
};
class compare
{
public:
bool operator()(const juese a, const juese b)const
{
return a.zhanlijieji > b.zhanlijieji;
}
};
void printset(set<juese,compare>&a) //遍历外面的
{
for (set<juese, compare>::const_iterator it = a.begin(); it!= a.end(); it++)
{
cout << "角色" << it->name << "是" << it->yuanshu << "属性角色" << "使用" <<it->wuqi << "武器" << endl;
}
}
void pushinsert(set<juese,compare>&a)
{
juese b;
a.insert(b);
}
int main()
{
int d;
cout << "输入要存入多少角色信息" << endl;
cin >> d;
set<juese,compare>a;
for (int i = 0; i < d; i++)
{
pushinsert(a);
}
printset(a);
system("pause");
return 0;
}
#include<iostream>
#include<set>
#include<string>
using namespace std;
class juese
{
public:
string name;
string yuanshu;
//int mingzuo;
string wuqi;
int zhanlijieji;
juese()
{
cout << "输入角色姓名" << endl;
cin >> name;
cout << "输入角色属性" << endl;
cin >> yuanshu;
//cout << "输入角色核心命坐" << endl;
//cin >> mingzuo;
cout << "输入角色武器" << endl;
cin >> wuqi;
cout << "输入角色强度阶级" << endl;
cin >> zhanlijieji;
}
/*void printset2() //遍历内置的
{
cout << "角色" << name << "是" << yuanshu << "属性角色,核心命坐为" << mingzuo << "命,使用"<<wuqi<< "武器" << endl;
}
bool operator()(juese a, juese b)
{
return a.zhanlijieji>b.zhanlijieji;
}*/
};
class compare
{
public:
bool operator()( juese &a, juese &b)
{
/*if (a.zhanlijieji == b.zhanlijieji)
{
return a.mingzuo > b.mingzuo;
}
else
{
return a.zhanlijieji > b.zhanlijieji;
}*/
return a.zhanlijieji > b.zhanlijieji;
}
};
int main()
{
set<juese,compare>a;
juese b;
juese c;
a.insert(b);
a.insert(c);
for (set<juese,compare>::iterator it = a.begin(); it != a.end(); it++)
{
cout << "角色" << it->name << "是" << it->yuanshu << "属性角色" << "使用" << it->wuqi << "武器" << endl;
}
system("pause");
return 0;
}
成功版本
#include<iostream>
#include<set>
#include<string>
using namespace std;
class juese
{
public:
string name;
string yuanshu;
int mingzuo;
string wuqi;
int zhanlijieji;
juese()
{
cout << "输入角色姓名" << endl;
cin >> name;
cout << "输入角色属性" << endl;
cin >> yuanshu;
cout << "输入角色核心命坐" << endl;
cin >> mingzuo;
cout << "输入角色武器" << endl;
cin >> wuqi;
cout << "输入角色强度阶级" << endl;
cin >> zhanlijieji;
}
/*void printset2() //遍历内置的
{
cout << "角色" << name << "是" << yuanshu << "属性角色,核心命坐为" << mingzuo << "命,使用"<<wuqi<< "武器" << endl;
}
bool operator()(juese a, juese b)
{
return a.zhanlijieji>b.zhanlijieji;
}*/
};
class compare
{
public:
bool operator()(juese a, juese b)const
{
if (a.zhanlijieji == b.zhanlijieji)
{
return a.mingzuo > b.mingzuo;
}
else
{
return a.zhanlijieji > b.zhanlijieji;
}
return a.zhanlijieji > b.zhanlijieji;
}
};
void printset(set<juese,compare>const &a) //遍历外面的
{
for (set<juese, compare>::const_iterator it = a.begin(); it != a.end(); it++)
{
cout << "角色" << it->name << "是" << it->yuanshu << "属性角色" << "使用" << it->wuqi << "武器" << endl;
}
}
int main()
{
set<juese,compare>a;
juese b;
juese c;
a.insert(b);
a.insert(c);
for (set<juese, compare>::const_iterator it = a.begin(); it != a.end(); it++)
{
cout << "角色" << it->name << "是" << it->yuanshu << "属性角色" << "使用" << it->wuqi << "武器" << endl;
}
system("pause");
return 0;
}
总结
-
bool operator()(juese a, juese b)const { if (a.zhanlijieji == b.zhanlijieji) { return a.mingzuo > b.mingzuo; } else { return a.zhanlijieji > b.zhanlijieji; } 注意 函数后加const 且 自定义类型不能加引用
-
for (set<juese, compare>::const_iterator it = a.begin(); it != a.end(); it++)
注意const必须有 没有跑不下来