set只有一个方法就是insert
#include<iostream>
#include<set>
//set和multiset是一个头文件
//set内部实现机制 红黑色(平衡二叉树的一种)
//关联式容器
//set不允许有重复元素
//multiset运行有重复元素
//容器查找效率高
//容器根据元素的值自动对元素排序 默认从小到大
using namespace std;
//仿函数
class mycompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void PrintSet(set<int> s1) {
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//set初始化
void test01() {
mycompare com;
com(10, 20); //仿函数
set<int> s1;
s1.insert(5);
s1.insert(2);
s1.insert(8);
s1.insert(1);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
//拷贝构造
set<int> s2(s1);
PrintSet(s2);
//赋值
set<int> s3;
s3 = s1;
//删除操作
s1.erase(s1.begin());
cout << "删除s1.begin()" << endl;
PrintSet(s1);
s1.erase(8);
cout << "删除8" << endl;
PrintSet(s1);
//如何改变默认排序?
//先序遍历 中序遍历 后序遍历
}
//set查找
void test02() {
//实值
set<int> s1;
s1.insert(5);
s1.insert(2);
s1.insert(8);
s1.insert(1);
PrintSet(s1);
set<int>::iterator ret = s1.find(8);
cout << "find 8" << endl;
if (ret == s1.end()) {
cout << "没有找到" << endl;
}
else {
cout << "ret:"<<*ret << endl;
}
//lower_bound:找第一个大于等于指定值的元素 返回为迭代器
cout << "lower_bound(大于等于) 4" << endl;
set<int>::iterator ret1 = s1.lower_bound(4);
if (ret1 == s1.end()) {
cout << "没有找到" << endl;
}
else {
cout << "ret1:" << *ret1 << endl;
}
//upper_bound:找第一个大于指定值的元素 返回为迭代器
cout << "upper_bound(大于) 2" << endl;
set<int>::iterator ret2 = s1.upper_bound(2);
if (ret2 == s1.end()) {
cout << "没有找到" << endl;
}
else {
cout << "ret2:" << *ret2 << endl;
}
//equal_range 返回lower_bound 和 upper_bound值
/*set<int>::iterator e1;
set<int>::iterator e2;
pair<e1, e2>=s1.equal_range(5); 错误做法*/
cout << "equal_range:返回lower_bound 和 upper_bound值 5" << endl;
pair<set<int>::iterator, set<int>::iterator> myret= s1.equal_range(5);
myret.first;
myret.second;
if (myret.first == s1.end()) {
cout << "没有找到" << endl;
}
else {
cout << "找到了" <<*myret.first<< endl;
}
if (myret.second == s1.end()) {
cout << "没有找到" << endl;
}
else {
cout << "找到了" << *myret.second << endl;
}
}
class Person {
public:
int id;
int age;
public:
Person(int Age,int Id):age(Age),id(Id){}
};
class mycompare2 {
public:
bool operator() (Person p1, Person p2) const{
return p1.age > p2.age; //用什么排序 把什么作为key关键字
}
};
void test03() {
//set<Person> sp;
//Person p1(20, 10), p2(30, 30), p3(34, 12);
//sp.insert(p1);
//sp.insert(p2);
//sp.insert(p3); //无法对p1,p2,p3进行排序 所以运行报错
set<Person,mycompare2> sp;
Person p1(20, 10), p2(30, 30), p3(34, 12);
Person p4(20, 10);
sp.insert(p1);
sp.insert(p2);
sp.insert(p3); //无法对p1,p2,p3进行排序 所以运行报错
for (set<Person, mycompare2>::iterator it = sp.begin(); it != sp.end(); it++) {
cout << (*it).age << " " << (*it).id << endl;
}
set<Person, mycompare2>::iterator ret = sp.find(p4);
if (ret == sp.end()) {
cout << "未找到" << endl;
}
else {
cout << "Age:"<<(*ret).age << endl;
}
}
int main(int) {
cout << "test01" << endl;
test01();
cout << "test02" << endl;
test02();
cout << "test03" << endl;
test03();
return 0;
}
#include<iostream>
#include<string>
using namespace std;
//类模板的实现需要指定类型
//函数模板的实现不需要指定类型
//类模板:template<class T1,class T2> struct pair;
void test01() {
//第一种方法创建一个对组 构造 方法
pair<string, int> pair1(string("name"), 1);
cout << pair1.first << endl;
cout << pair1.second << endl;
//第二种
pair<string, int> pair2 = make_pair("name", 30);
cout << pair2.first << endl;
cout << pair2.second << endl;
//pair= 赋值
pair<string, int> pair3 = pair2;
cout << pair3.first << endl;
cout << pair3.second << endl;
}
int main() {
test01();
return 0;
}