目录
3.8 set/multiset 容器3.8.1 set基本概念3.8.2 set构造和赋值3.8.3 set大小和交换3.8.4 set插入和删除3.8.5 set查找和统计3.8.6 set和multiset区别3.8.7 pair对组创建3.8.8 set容器排序1.内置数据类型升降序排序2.自定义数据类型排序
3.8 set/multiset 容器
3.8.1 set基本概念
本质:
set/mulitiset 属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:
set不允许容器中有重复的元素。
multiset允许容器中有重复的元素。
3.8.2 set构造和赋值
#include <iostream>
#include <set>
using namespace std
;
void printSet(set
<int> s
)
{
for(set
<int>::iterator it
= s
.begin(); it
!= s
.end(); it
++)
{
cout
<< (*it
) << " " << endl
;
}
}
void test01()
{
set
<int> s1
;
s1
.insert(30);
s1
.insert(10);
s1
.insert(20);
s1
.insert(40);
s1
.insert(20);
printSet(s1
);
set
<int>s2(s1
);
printSet(s2
);
set
<int>s3
;
s3
= s2
;
printSet(s3
);
}
int main()
{
test01();
system("pause");
return 0;
}
3.8.3 set大小和交换
函数原型:
size();
empty();
swap(st
);
3.8.4 set插入和删除
函数原型:
insert(elem
);
clear();
erase(pos
);
erase(beg
,end
);
erase(elem
);
3.8.5 set查找和统计
函数原型:
find(key
);
count(key
);
————————————————————————————————————————————————————————————————————————————————————————————
#include <set>
#include <iostream>
using namespace std
;
void test01()
{
set
<int> s1
;
s1
.insert(10);
s1
.insert(30);
s1
.insert(20);
s1
.insert(40);
set
<int>::iterator pos
= s1
.find(30);
if (pos
!= s1
.end())
{
cout
<< "找到元素:" << *pos
<< endl
;
}
else
{
cout
<< "未找到元素" << endl
;
}
}
void test02()
{
set
<int> s1
;
s1
.insert(10);
s1
.insert(30);
s1
.insert(20);
s1
.insert(40);
int num
= s1
.count(30);
}
int main()
{
test01();
system("pause");
return 0;
}
3.8.6 set和multiset区别
区别:
·set不可以插入重复数据,而multiset可以
·set插入数据的同时会返回插入结果,表示插入是否成功
·multiset不会检测数据,因此可以插入重复数据
#include <set>
#include <iostream>
using namespace std
;
void test01()
{
set
<int>s
;
pair
<set
<int>::iterator
,bool> ret
= s
.insert(10);
if(ret
.second
)
{
cout
<< "Success" << endl
;
}
else
{
cout
<< "Fail" << endl
;
}
multiset
<int>ms
;
ms
.insert(10);
ms
.insert(10);
ms
.insert(10);
ms
.insert(10);
for (multiset
<int>::iterator it
= ms
.begin(); it
!= ms
.end(); it
++)
{
cout
<< *it
<< " ";
}
cout
<< endl
;
}
int main()
{
test01();
system("pause");
return 0;
}
3.8.7 pair对组创建
功能描述: 成对出现的数据,利用对组可以返回两个数据
两种创建方式:
pair
<type
, type
> p
( value1
,value2
);
pair
<type
, type
> p
= make pair
( value1
, value2
);
—————————————————————————————————————————————————————————————————————————————————————————————
#include <string>
#include <iostream>
using namespace std
;
void test01()
{
pair
<string
,int>p
("Tom", 20);
cout
<< "姓名:" << p
.first
<< " 年龄:" << p
.second
<< endl
;
pair
<string
,int>p2
= make_pair("Jerry",19);
cout
<< "姓名:" << p2
.first
<< " 年龄:" << p2
.second
<< endl
;
}
int main()
{
test01();
system("pause");
return 0;
}
3.8.8 set容器排序
1.内置数据类型升降序排序
#include <iostream>
#include <string>
#include <set>
using namespace std
;
class MyCompare
{
public:
bool operator()(int v1
, int v2
)
{
return v1
>v2
;
}
};
void test01()
{
set
<int> s1
;
s1
.insert(10);
s1
.insert(40);
s1
.insert(20);
s1
.insert(30);
s1
.insert(50);
for(set
<int>::iterator it
= s1
.begin(); it
!= s1
.end(); it
++)
{
cout
<< *it
<< " ";
}
cout
<< endl
;
set
<int,MyCompare
> s2
;
s2
.insert(10);
s2
.insert(40);
s2
.insert(20);
s2
.insert(30);
s2
.insert(50);
for(set
<int,MyCompare
>::iterator it
= s2
.begin(); it
!= s2
.end(); it
++)
{
cout
<< *it
<< " ";
}
cout
<< endl
;
}
int main()
{
test01();
system("pause");
return 0;
}
2.自定义数据类型排序
#include <iostream>
#include <string>
#include <set>
using namespace std
;
class Person
{
public:
Person
(string name
, int age
)
{
this->m_Name
= name
;
this->m_Age
= age
;
}
string m_Name
;
int m_Age
;
};
class comparePerson
{
public:
bool operator()(const Person
& p1
, const Person
& p2
)
{
return p1
.m_Age
> p2
.m_Age
;
}
};
void test02()
{
set
<Person
,comparePerson
> s
;
Person
p1("kk",21);
Person
p2("bb",22);
Person
p3("cc",23);
Person
p4("dd",25);
s
.insert(p1
);
s
.insert(p2
);
s
.insert(p3
);
s
.insert(p4
);
for(set
<Person
,comparePerson
>::iterator it
= s
.begin(); it
!= s
.end(); it
++)
{
cout
<< (*it
).m_Name
<< " " << (*it
).m_Age
<< endl
;
}
}
int main()
{
test02();
system("pause");
return 0;
}