set容器与multiset容器

set的特性是所有元素会根据元素的值进行自动排序。set是以RB-tree(红黑树,平衡二叉树的一种)为底层机制,其查找效率非常好,set容器中不允许重复元素,multiset允许重复元素

只能通过insert插入元素;不能通过迭代器改变元素的值,若要改变,则需要删除原子结点,后插入新的子节点。这与底层实现有关

#include <iostream>
#include <set>
//set和multiset是同一个头文件
using namespace std;

//容器初始化
void text1()
{
set<int> s;//排序默认从小到大
s.insert(7);
s.insert(2);
s.insert(4);
s.insert(5);
s.insert(1);
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
    cout<<*it<<" ";
}
cout<<endl;
//赋值
set<int> s2;
s2=s;
s2.swap(s);
//删除操作
s.erase(s.begin());
s.erase(7);//删除第一次出现的7;
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
    cout<<*it<<" ";
}
/*
1 2 4 5 7
2 4 5
*/

}

//查找
void text2()
{
set<int> s;
s.insert(7);
s.insert(2);
s.insert(4);
s.insert(5);
s.insert(1);
set<int>::iterator it=s.find(4);//查找值是否存在,存在则返回其迭代器,否则返回最后一个元素后一个位置的迭代器
if(it!=s.end())
{
    cout<<"it:"<<*it<<endl;
}
else
{
    cout<<"no find"<<endl;
}
//找到第一个大于等于2的值
it=s.lower_bound(2);//返回第一个大于等于2的值的迭代器
if(it!=s.end())
{
    cout<<"it:"<<*it<<endl;
}
else
{
    cout<<"no find"<<endl;
}
it=s.upper_bound(2);//找到第一个大于2的值,返回迭代器
if(it!=s.end())
{
    cout<<"it:"<<*it<<endl;
}
else
{
    cout<<"no find"<<endl;
}

//equual_range 返回lower_bound 和 upper_bound的迭代器

pair<set<int>::iterator,set<int>::iterator> myret=s.equal_range(2);
if(myret.first==s.end())
{
    cout<<"no find"<<endl;
}
else
{
    cout<<"myret:"<<*(myret.first)<<endl;
}
if(myret.second==s.end())
{
    cout<<"no find"<<endl;
}
else
{
    cout<<"myret:"<<*(myret.second)<<endl;
}

/*
it:4
it:2
it:4
myret:2
myret:4
*/
}


int main()
{
    //text1();
    text2();
    //test4();
    return 0;
}

存储自定义类型

#include <iostream>
#include <set>
//set和multiset是同一个头文件
using namespace std;
//仿函数
class mycompare
{
    public:
        bool operator() (int v1,int v2)
        {
            return v1>v2;
        }
};

void test()
{
    
set<int,mycompare> s;//排序默认从小到大,重载类,排序从大到小
s.insert(7);
s.insert(2);
s.insert(4);
s.insert(5);
s.insert(1);
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
    cout<<*it<<" ";
}
/*
7 5 4 2 1
*/
}

class person
{
    public:
        person(int id,int age):id(id),age(age)
        {
        }
    public:
        int id;
        int age;
        
};

class mycompare2
{
    public:
        bool operator() (person p1,person p2) const
        {
            return p1.age>p2.age;
        }
    
};
void text2()
{
    set<person,mycompare2> sp;
    person p1(10,20),p2(30,40),p3(50,60);
    sp.insert(p1);
    sp.insert(p2);
    sp.insert(p3);
    person p4(10,40);
    for(set<person,mycompare2>::iterator it=sp.begin();it!=sp.end();it++)
    {
        cout<<(*it).age<<" "<<(*it).id<<endl;
    }
/*
60 50
40 30
20 10
*/
//查找,此时会更据排序的参数进行查找,此处为age;
set<person,mycompare2>::iterator it=sp.find(p4);
if(it==sp.end())
{
    cout<<"no find"<<endl;
}
else
{
    cout<<" find:"<<(*it).id<<" "<<(*it).age<<endl;
}
// find:30 40
}
int main()
{
    //test();
    text2();
}


#include <iostream>
#include <set>
//set和multiset是同一个头文件
using namespace std;

//容器初始化
void text1()
{
set<int> s;//排序默认从小到大
s.insert(7);
s.insert(2);
s.insert(4);
s.insert(5);
s.insert(1);
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
	cout<<*it<<" ";
}
cout<<endl;
//赋值
set<int> s2;
s2=s;
s2.swap(s);
//删除操作
s.erase(s.begin());
s.erase(7);//删除第一次出现的7;
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
	cout<<*it<<" ";
}
/*
1 2 4 5 7
2 4 5
*/

}

//查找
void text2()
{
set<int> s;
s.insert(7);
s.insert(2);
s.insert(4);
s.insert(5);
s.insert(1);
set<int>::iterator it=s.find(4);//查找值是否存在,存在则返回其迭代器,否则返回最后一个元素后一个位置的迭代器
if(it!=s.end())
{
	cout<<"it:"<<*it<<endl;
}
else
{
	cout<<"no find"<<endl;
}
//找到第一个大于等于2的值
it=s.lower_bound(2);//返回第一个大于等于2的值的迭代器
if(it!=s.end())
{
	cout<<"it:"<<*it<<endl;
}
else
{
	cout<<"no find"<<endl;
}
it=s.upper_bound(2);//找到第一个大于2的值,返回迭代器
if(it!=s.end())
{
	cout<<"it:"<<*it<<endl;
}
else
{
	cout<<"no find"<<endl;
}

//equual_range 返回lower_bound 和 upper_bound的迭代器

pair<set<int>::iterator,set<int>::iterator> myret=s.equal_range(2);
if(myret.first==s.end())
{
	cout<<"no find"<<endl;
}
else
{
	cout<<"myret:"<<*(myret.first)<<endl;
}
if(myret.second==s.end())
{
	cout<<"no find"<<endl;
}
else
{
	cout<<"myret:"<<*(myret.second)<<endl;
}

/*
it:4
it:2
it:4
myret:2
myret:4
*/
}


int main()
{
	//text1();
	text2();
	//test4();
	return 0;
}



存储自定义类型

#include <iostream>
#include <set>
//set和multiset是同一个头文件
using namespace std;
//仿函数
class mycompare
{
	public:
		bool operator() (int v1,int v2)
		{
			return v1>v2;
		}
};

void test()
{
	
set<int,mycompare> s;//排序默认从小到大,重载类,排序从大到小
s.insert(7);
s.insert(2);
s.insert(4);
s.insert(5);
s.insert(1);
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
	cout<<*it<<" ";
}
/*
7 5 4 2 1
*/
}

class person
{
	public:
		person(int id,int age):id(id),age(age)
		{
		}
	public:
		int id;
		int age;
		
};

class mycompare2
{
	public:
		bool operator() (person p1,person p2) const
		{
			return p1.age>p2.age;
		}
	
};
void text2()
{
	set<person,mycompare2> sp;
	person p1(10,20),p2(30,40),p3(50,60);
	sp.insert(p1);
	sp.insert(p2);
	sp.insert(p3);
	person p4(10,40);
	for(set<person,mycompare2>::iterator it=sp.begin();it!=sp.end();it++)
	{
		cout<<(*it).age<<" "<<(*it).id<<endl;
	}
/*
60 50
40 30
20 10
*/
//查找,此时会更据排序的参数进行查找,此处为age;
set<person,mycompare2>::iterator it=sp.find(p4);
if(it==sp.end())
{
	cout<<"no find"<<endl;
}
else
{
	cout<<" find:"<<(*it).id<<" "<<(*it).age<<endl;
}
// find:30 40
}
int main()
{
	//test();
	text2();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值