set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列。元素插入过程是按排序规则插入,所以不能指定插入位置。
set采用红黑树变体的数据结构实现,红黑树属于平衡二叉树。在插入操作和删除操作上比vector快。
set不可以直接存取元素。(不可以使用at.(pos)与[]操作符)。
multiset与set的区别:set支持唯一键值,每个元素值只能出现一次;而multiset中同一值可以出现多次。
不可以直接修改set或multiset容器中的元素值,因为该类容器是自动排序的。如果希望修改一个元素值,必须先删除原有的元素,再插入新的元素。
#include
仿函数 重载了()函数调用符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "set"
//1 集合 元素唯一 自动排序(默认情况下 是从小到大) 不能按照[]方式插入元素
// 红黑树
//set元素的添加/遍历/删除基本操作
void main91()
{
set<int> set1;
for (int i=0; i<5; i++)
{
int tmp = rand();
set1.insert(tmp);
}
//插入元素 重复的
set1.insert(100);
set1.insert(100);
set1.insert(100);
for (set<int>::iterator it=set1.begin(); it!=set1.end(); it++ )
{
cout << *it << " ";
}
//删除集合
while ( !set1.empty())
{
set<int>::iterator it = set1.begin();
cout << *it << " ";
set1.erase(set1.begin());
}
}
//2 对基本的数据类型 set能自动的排序
void main92()
{
set<int> set1;
set<int, less<int>> set2; //默认情况下是这样
set<int, greater<int>> set3; //从大 到 小
for (int i=0; i<5; i++)
{
int tmp = rand();
set3.insert(tmp);
}
//从大 到 小
for (set<int, greater<int>>::iterator it = set3.begin(); it != set3.end(); it++ )
{
cout << *it << endl;
}
}
class Student
{
public:
Student(char *name, int age)
{
strcpy(this->name, name);
this->age = age;
}
public:
char name[64];
int age;
};
//仿函数
struct FuncStudent
{
bool operator()(const Student &left, const Student &right)
{
if (left.age < right.age) //如果左边的小 就返回真 从小到大按照年龄进行排序
{
return true;
}
else
{
return false;
}
}
};
//3 自定义数据类型的排序 仿函数的用法
void main93()
{
Student s1("s1", 31);
Student s2("s2", 22);
Student s3("s3", 44);
Student s4("s4", 11);
Student s5("s5", 31);
set<Student, FuncStudent> set1;
set1.insert(s1);
set1.insert(s2);
set1.insert(s3);
set1.insert(s4);
set1.insert(s5); //如果两个31岁 能插入成功
//如何知道 插入 的结果
//遍历
for (set<Student, FuncStudent>::iterator it=set1.begin(); it!=set1.end(); it++ )
{
cout << it->age << "\t" << it->name << endl;
}
}
//typedef pair<iterator, bool> _Pairib;
//4 如何判断 set1.insert函数的返回值
//Pair的用法
void main94()
{
Student s1("s1", 31);
Student s2("s2", 22);
Student s3("s3", 44);
Student s4("s4", 11);
Student s5("s5", 31);
set<Student, FuncStudent> set1;
pair<set<Student, FuncStudent>::iterator, bool> pair1 = set1.insert(s1);
if (pair1.second == true)
{
cout << "插入s1成功" << endl;
}
else
{
cout << "插入s1失败" << endl;
}
set1.insert(s2);
//如何知道 插入 的结果
pair<set<Student, FuncStudent>::iterator, bool> pair5 = set1.insert(s5); //如果两个31岁 能插入成功
if (pair5.second == true)
{
cout << "插入s1成功" << endl;
}
else
{
cout << "插入s1失败" << endl;
}
//遍历
for (set<Student, FuncStudent>::iterator it=set1.begin(); it!=set1.end(); it++ )
{
cout << it->age << "\t" << it->name << endl;
}
}
//find查找 equal_range
//返回结果是一个pair
void main95()
{
set<int> set1;
for (int i=0; i<10; i++)
{
set1.insert(i+1);
}
//从大 到 小
for (set<int>::iterator it = set1.begin(); it != set1.end(); it++ )
{
cout << *it << " ";
}
cout << endl;
set<int>::iterator it0 = set1.find(5);
cout << "it0:" << *it0 << endl;
int num1 = set1.count(5);
cout << "num1:" << num1 << endl;
set<int>::iterator it1 = set1.lower_bound(5); // 大于等于5的元素 的 迭代器的位置
cout << "it1:" << *it1 << endl;
set<int>::iterator it2 = set1.lower_bound(5); // 大于5的元素 的 迭代器的位置
cout << "it2:" << *it2 << endl;
//
//typedef pair<iterator, bool> _Pairib;
//typedef pair<iterator, iterator> _Pairii;
//typedef pair<const_iterator, const_iterator> _Paircc;
//把5元素删除掉
set1.erase(5);
pair<set<int>::iterator, set<int>::iterator> mypair = set1.equal_range(5);
set<int>::iterator it3 = mypair.first;
cout << "it3:" << *it3 << endl; //5 //6
set<int>::iterator it4 = mypair.second;
cout << "it4:" << *it4 << endl; //6 //6
}
void main999()
{
//main91();
//main92();
//main93();
//main94();
//main95();
cout<<"hello..."<<endl;
system("pause");
return ;
}
#include <iostream>
using namespace std;
#include "set"
void main1001()
{
multiset<int> set1;
int tmp = 0;
printf("请输入multiset集合的值:");
scanf("%d", &tmp);
while (tmp != 0)
{
set1.insert(tmp);
printf("请输入multiset集合的值:");
scanf("%d", &tmp);
}
//遍历
for (multiset<int>::iterator it=set1.begin(); it!=set1.end(); it++ )
{
cout << *it << " ";
}
cout << endl;
while (!set1.empty())
{
multiset<int>::iterator it = set1.begin();
cout << *it << " ";
set1.erase(it);
}
}
void main()
{
main1001();
cout<<"hello..."<<endl;
system("pause");
return ;
}