Day 27 C++ set/ multiset 容器,pair对组

set容器(集合)基本概念

定义

set 是 C++ 标准库中的一个容器,它实现了一个有序的、不重复的集合。每个元素在 set 中只能出现一次,并且按照一定的排序规则(默认排序规则为从小到大)进行排序。

特点

  1. 有序性set 中的元素是按照内部定义的排序规则进行排序的。默认情况下,元素按照升序进行排序,但也可以自定义排序规则。
  2. 唯一性set 中的元素是唯一的,重复的元素将会被自动过滤掉。在 set 中插入重复元素将不会改变集合的内容。
  3. 底层数据结构set 通常使用平衡二叉搜索树(红黑树)作为底层数据结构来实现有序性和唯一性的特性。这使得插入、查找和删除操作的平均时间复杂度都为 O(log n)。
  4. 迭代器:可读迭代器,可以遍历 set 中的元素。迭代器按照元素的排序顺序依次访问集合中的元素。
  5. 查找操作set 提供了高效的查找操作。可以使用成员函数 find() 来查找特定的元素。由于 set 是有序的,这个操作的平均时间复杂度为 O(log n)。
  6. 插入操作:可以使用成员函数 insert()set 中插入元素。如果插入的元素已经存在,那么插入操作将被忽略。
  7. 删除操作:可以使用成员函数 erase() 来删除 set 中的元素。可以指定要删除的元素或者通过迭代器删除元素。

set构造和赋值

构造
  • set<T> st; //默认构造函数:
  • set(const set &st); //拷贝构造函数
赋值
  • set& operator=(const set &st); //重载等号操作符
总结
  • set容器插入数据时用insert
  • set容器插入数据的数据会自动排序(升序)
示例
#include <iostream>
#include <set>

int main() {
  // 默认构造函数
  std::set<int> mySet; // 创建一个空的set容器

  // 拷贝构造函数
  std::set<int> 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


set大小和交换

函数原型
  • size(); //返回容器中元素的数目
  • empty(); //判断容器是否为空
  • swap(st); //交换两个集合容器
示例
#include <iostream>
#include <set>

int main() {
  std::set<int> 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<int> 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


set插入和删除

函数原型
  • insert(elem); //在容器中插入元素。插入数据的同时会返回插入结果,表示插入是否成功
  • clear(); //清除所有元素
  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  • erase(elem); //删除容器中值为elem的元素。返回下一个元素的迭代器。如果删除的是最后一个元素或者删除了全部元素,则返回指向set容器中的末尾位置的迭代器。
示例
#include <iostream>
#include <set>

int main() {
  std::set<int> 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


set查找和统计

函数原型
  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器若不存在,返回set.end();
  • count(key); //统计key的元素个数
示例
#include <iostream>
#include <set>

int main() {
  std::set<int> 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


set容器排序

利用仿函数改变排序规则
#include <iostream>
#include <set>

// 仿函数,用于定义自定义比较函数
struct MyComparator {
    bool operator() (int a, int b) const {
        // 自定义的排序规则,按照绝对值从大到小排序
        return abs(a) > abs(b);
    }
};

int main() {
    // 使用自定义比较函数的 std::set
    std::set<int, MyComparator> 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,以改变排序规则。


set存放自定义数据类型
#include <set>
#include <string>

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<Person, comparePerson> 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<Person, comparePerson>::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容器(多重集合)

定义

multiset 是 C++ 标准库中的一个容器,它实现了一个有序的、可以包含重复元素的集合。与 set 不同的是,multiset 允许存储相同的元素,并且仍然按照一定的排序规则进行排序。(可以含有重复元素的set)

multiset 不支持随机访问,没有索引,不能跳跃访问 ,也不可以通过[]或者at方式访问数据。它是一个有序容器,元素存储在内部的平衡二叉搜索树中,按照指定的排序规则进行排序。

set和multiset异同

相同
  • set/multiset都属于关联式容器,底层结构是用二叉树实现。
  • 容器中,元素即为键值(key value),键值既是键,也是实际存储的值。键用于定义元素的排序顺序,决定了元素在容器中的位置。
  • 都不能通过迭代器去修改元素,原因是修改元素会破坏容器组织。只能先删除再添加。
  • 元素插入过程是按排序规则插入,所以不能指定插入位置。
  • 不可以直接存取元素。(不可以使用at.(pos)与[]操作符)
  • 它们对容器的操作方法几乎一模一样容器特点也差不多一致
区别
  • set不可以插入重复数据,而multiset不会检测数据,因此可以插入重复数据

示例

#include <set>

//set和multiset区别
void test01()
{
	set<int> s;
	pair<set<int>::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<int> ms;
	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;
}

总结
  • 如果不允许插入重复数据可以利用set
  • 如果需要插入重复数据利用multiset

pair对组

定义

pair 是 C++ 标准库提供的一个模板类,用来表示一对值(对组)。它可以包含任意两个值,并对这两个值进行组合,利用对组可以返回两个数据。

两种创建方式

  • pair<type, type> p ( value1, value2 );
  • pair<type, type> p = make_pair( value1, value2 );

示例

#include <string>

//对组创建
void test01()
{
	pair<string, int> p(string("Tom"), 20);
	cout << "姓名: " <<  p.first << " 年龄: " << p.second << endl;

	pair<string, int> p2 = make_pair("Jerry", 10);
	cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_74921567/article/details/132223719