set
是 C++ 标准库中的一个容器,它实现了一个有序的、不重复的集合。每个元素在 set
中只能出现一次,并且按照一定的排序规则(默认排序规则为从小到大)进行排序。
set
中的元素是按照内部定义的排序规则进行排序的。默认情况下,元素按照升序进行排序,但也可以自定义排序规则。set
中的元素是唯一的,重复的元素将会被自动过滤掉。在 set
中插入重复元素将不会改变集合的内容。set
通常使用平衡二叉搜索树(红黑树)作为底层数据结构来实现有序性和唯一性的特性。这使得插入、查找和删除操作的平均时间复杂度都为 O(log n)。set
中的元素。迭代器按照元素的排序顺序依次访问集合中的元素。set
提供了高效的查找操作。可以使用成员函数 find()
来查找特定的元素。由于 set
是有序的,这个操作的平均时间复杂度为 O(log n)。insert()
向 set
中插入元素。如果插入的元素已经存在,那么插入操作将被忽略。erase()
来删除 set
中的元素。可以指定要删除的元素或者通过迭代器删除元素。set st;
//默认构造函数:set(const set &st);
//拷贝构造函数set& operator=(const set &st);
//重载等号操作符#include
#include
int main() {
// 默认构造函数
std::set mySet; // 创建一个空的set容器
// 拷贝构造函数
std::set anotherSet(mySet); // 使用一个已存在的set容器创建另一个set容器
// 插入数据
mySet.insert(5);
mySet.insert(2);
mySet.insert(8);
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
// 重载等号操作符赋值
anotherSet = mySet;
for (const auto& element : anotherSet) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
输出
2 5 8
2 5 8
size();
//返回容器中元素的数目empty();
//判断容器是否为空swap(st);
//交换两个集合容器#include
#include
int main() {
std::set mySet;
// 判断容器是否为空
if (mySet.empty()) {
std::cout << "Set is empty" << std::endl;
} else {
std::cout << "Set is not empty" << std::endl;
}
// 插入数据
mySet.insert(5);
mySet.insert(2);
mySet.insert(8);
// 返回容器中元素的数目
std::cout << "Size of set: " << mySet.size() << std::endl;
// 交换两个集合容器
std::set anotherSet;
anotherSet.insert(10);
anotherSet.insert(20);
mySet.swap(anotherSet);
std::cout << "Elements in mySet after swapping: ";
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
std::cout << "Elements in anotherSet after swapping: ";
for (const auto& element : anotherSet) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
输出
Set is empty
Size of set: 3
Elements in mySet after swapping: 10 20
Elements in anotherSet after swapping: 2 5 8
insert(elem);
//在容器中插入元素。插入数据的同时会返回插入结果,表示插入是否成功clear();
//清除所有元素erase(pos);
//删除pos迭代器所指的元素,返回下一个元素的迭代器。erase(beg, end);
//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。erase(elem);
//删除容器中值为elem的元素。返回下一个元素的迭代器。如果删除的是最后一个元素或者删除了全部元素,则返回指向set容器中的末尾位置的迭代器。#include
#include
int main() {
std::set mySet;
// 插入元素并返回插入结果
auto result = mySet.insert(1);
if (result.second) {
std::cout << "Insertion successful!" << std::endl;
} else {
std::cout << "Insertion failed!" << std::endl;
}
// 清除所有元素
mySet.clear();
// 插入一些元素
mySet.insert(2);
mySet.insert(3);
mySet.insert(4);
mySet.insert(5);
// 删除指定位置的元素,并获取返回的下一个元素的迭代器
auto it = mySet.erase(mySet.begin());
std::cout << "After erasing the first element: ";
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
// 删除区间 [2, 4) 的所有元素,并获取返回的下一个元素的迭代器
it = mySet.erase(mySet.find(2), mySet.find(4));
std::cout << "After erasing elements in the range [2, 4): ";
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
// 删除值为 5 的元素,并获取返回的下一个元素的迭代器
it = mySet.erase(5);
std::cout << "After erasing the element with value 5: ";
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
// 输出返回的迭代器指向的元素
if (it != mySet.end()) {
std::cout << "Iterator points to: " << *it << std::endl;
} else {
std::cout << "Iterator points to end" << std::endl;
}
return 0;
}
输出
Insertion successful!
After erasing the first element: 3 4 5
After erasing elements in the range [2, 4): 5
After erasing the element with value 5:
Iterator points to end
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();count(key);
//统计key的元素个数#include
#include
int main() {
std::set mySet;
// 插入一些元素
mySet.insert(1);
mySet.insert(2);
mySet.insert(3);
mySet.insert(4);
mySet.insert(5);
// 查找元素
auto it = mySet.find(3);
if (it != mySet.end()) {
std::cout << "Element found: " << *it << std::endl;
} else {
std::cout << "Element not found" << std::endl;
}
// 统计元素个数
int count = mySet.count(5);
std::cout << "Number of elements with value 5: " << count << std::endl;
return 0;
}
输出
Element found: 3
Number of elements with value 5: 1
#include
#include
// 仿函数,用于定义自定义比较函数
struct MyComparator {
bool operator() (int a, int b) const {
// 自定义的排序规则,按照绝对值从大到小排序
return abs(a) > abs(b);
}
};
int main() {
// 使用自定义比较函数的 std::set
std::set mySet;
// 向 set 容器中插入元素
mySet.insert(-5);
mySet.insert(10);
mySet.insert(-3);
mySet.insert(8);
// 输出 set 容器中的元素
for (int num : mySet) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
输出
-3 -5 8 10
在上述示例中,通过定义 MyComparator 结构体作为仿函数,并重载 () 运算符来实现自定义的排序规则。在这个例子中,我们按照绝对值从大到小排序,然后将 MyComparator 作为第二个模板参数传递给 std::set,以改变排序规则。
#include
#include
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 test01()
{
set s;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
对于自定义数据类型,set必须指定排序规则才可以插入数据
multiset
是 C++ 标准库中的一个容器,它实现了一个有序的、可以包含重复元素的集合。与 set
不同的是,multiset
允许存储相同的元素,并且仍然按照一定的排序规则进行排序。(可以含有重复元素的set)
multiset
不支持随机访问,没有索引,不能跳跃访问 ,也不可以通过[]或者at方式访问数据。它是一个有序容器,元素存储在内部的平衡二叉搜索树中,按照指定的排序规则进行排序。
#include
//set和multiset区别
void test01()
{
set s;
pair::iterator, bool> ret = s.insert(10);
if (ret.second) {
cout << "第一次插入成功!" << endl;
}
else {
cout << "第一次插入失败!" << endl;
}
ret = s.insert(10);
if (ret.second) {
cout << "第二次插入成功!" << endl;
}
else {
cout << "第二次插入失败!" << endl;
}
//multiset
multiset ms;
ms.insert(10);
ms.insert(10);
for (multiset::iterator it = ms.begin(); it != ms.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
pair 是 C++ 标准库提供的一个模板类,用来表示一对值(对组)。它可以包含任意两个值,并对这两个值进行组合,利用对组可以返回两个数据。
pair p ( value1, value2 );
pair p = make_pair( value1, value2 );
#include
//对组创建
void test01()
{
pair p(string("Tom"), 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
pair p2 = make_pair("Jerry", 10);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main() {
test01();
system("pause");
return 0;
}