3.8 set/multiset 容器

本文详细介绍了C++中set容器的构造方法、插入与排序特性,以及拷贝构造和赋值操作。通过实例展示了如何使用set存储并遍历整数,并探讨了set自动去重和保持有序的特点。

 

#include<iostream>
#include<set>
using namespace std;
//set容器构造和赋值

void printSet(set<int>&s)
{
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
	{	
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int>s1; 
	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(40);
	//遍历容器
	//set容器特点:所有元素插入时候自动被排序
	//set容器不允许插入重复值
	printSet(s1);

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

	//赋值
	set<int>s3; 
	s3 = s2;
	printSet(s3);
}

int main()
{
	test01();
	system("pause");
	return 0;
}

 

基本语法与结构 变量定义与基本数据类型(int, float, double, char, bool) 类型推导(auto, decltype) 运算符(算术、逻辑、比较、位运算等) 控制结构:if-else, switch, for, while, do-while 函数定义、参数传递(值传递、引用传递、指针传递) 函数重载与默认参数 const 关键字的使用(常量、常量成员函数) 2. 面向对象编程(OOP) 类与对象的定义 构造函数与析构函数 成员访问修饰符(public, private, protected) 封装、继承、多态(虚函数、抽象类) 运算符重载 this 指针 静态成员(变量与函数) 友元函数与友元类 3. 指针与引用 指针的基本操作(定义、赋值、解引用) 指针与数组的关系 引用的使用(别名机制) 指向函数的指针 nullptr 的使用 4. 内存管理 动态内存分配(new / delete, new[] / delete[]) 内存泄漏与如何避免 智能指针(std::unique_ptr, std::shared_ptr, std::weak_ptr) RAII(资源获取即初始化)模式 5. 标准模板库(STL) 容器: 序列容器:vector, list, deque 关联容器set, map, multiset, multimap 无序容器:unordered_set, unordered_map 迭代器(begin(), end(), rbegin(), rend()) 算法(sort, find, copy, transform 等) 函数对象(Functor)与 Lambda 表达式 std::pair, std::tuple, std::optional7. 命名空间 namespace 的定义与使用 嵌套命名空间 using 声明与 using namespace 8. 异常处理 try, catch, throw 异常安全编程 noexcept 说明符 9. 预处理器与宏 #include, #define, #ifdef, #ifndef 宏定义与函数宏 防止头文件重复包含(#pragma once 或 #ifndef ... #define ...) 10. 文件操作 输入输出流(iostream, fstream) 文件读写(ifstream, ofstream, fstream) 文本与二进制模式 11. 类型转换 static_cast dynamic_cast reinterpret_cast const_cast 给出一个详细的类保护上述知识点
最新发布
08-08
下面是一个 **完整的 C++ 类示例**,它 **系统性地涵盖了你列出的 C++ 所有基础知识**,包括: - 基本语法与结构 - 面向对象编程(OOP) - 指针与引用 - 内存管理 - STL 容器与算法 - 命名空间 - 异常处理 - 宏定义与预处理器 - 文件操作 - 类型转换 --- ### ✅ 示例类:`PersonManager` ```cpp #include <iostream> #include <vector> #include <map> #include <list> #include <memory> #include <string> #include <fstream> #include <algorithm> #include <stdexcept> #include <typeinfo> #include <cassert> using namespace std; // 防止头文件重复包含 #ifndef PERSON_H #define PERSON_H // 枚举类型 enum class Gender { MALE, FEMALE, OTHER }; // 命名空间 namespace MyNamespace { // 基类 Person class Person { protected: string name; int age; Gender gender; public: // 构造函数 Person(string n, int a, Gender g) : name(n), age(a), gender(g) {} // 虚函数实现多态 virtual void introduce() const { cout << "我是 " << name << ",年龄 " << age << "。"; } // 纯虚函数,使类成为抽象类 virtual void work() const = 0; // const 成员函数 string getName() const { return name; } // this 指针使用示例 Person& setAge(int a) { this->age = a; return *this; } // 静态成员函数 static Gender getGenderEnum(const string& genderStr) { if (genderStr == "MALE") return Gender::MALE; if (genderStr == "FEMALE") return Gender::FEMALE; return Gender::OTHER; } // 虚析构函数(RAII) virtual ~Person() {} }; // 派生类 Student class Student : public Person { private: string major; double gpa; public: Student(string n, int a, Gender g, string m, double gpa) : Person(n, a, g), major(m), gpa(gpa) {} void introduce() const override { Person::introduce(); cout << "专业是 " << major << ",GPA: " << gpa << endl; } void work() const override { cout << name << " 的工作是学习。" << endl; } // 函数重载 void work(const string& course) const { cout << name << " 正在学习课程: " << course << endl; } }; // 友元类示例 class PersonManager; class Friend { public: void accessPrivate(PersonManager& pm); }; // 管理类 class PersonManager { private: vector<shared_ptr<Person>> people; map<string, shared_ptr<Person>> personMap; public: // 添加人员 void addPerson(const shared_ptr<Person>& person) { people.push_back(person); personMap[person->getName()] = person; } // 使用 Lambda 表达式排序 void sortPeopleByAge() { sort(people.begin(), people.end(), [](const shared_ptr<Person>& a, const shared_ptr<Person>& b) { return a->getName() < b->getName(); }); } // 使用迭代器遍历 void displayAll() const { for (auto it = people.begin(); it != people.end(); ++it) { (*it)->introduce(); } } // 异常处理 shared_ptr<Person> findPerson(const string& name) const { auto it = personMap.find(name); if (it == personMap.end()) { throw runtime_error("找不到姓名为 " + name + " 的人"); } return it->second; } // RAII 示例:文件读写 void saveToFile(const string& filename) const { ofstream out(filename); if (!out) { throw runtime_error("无法打开文件进行写入"); } for (const auto& p : people) { out << p->getName() << endl; } } void loadFromFile(const string& filename) { ifstream in(filename); if (!in) { throw runtime_error("无法打开文件进行读取"); } string name; while (getline(in, name)) { // 使用智能指针管理内存 people.push_back(make_shared<Student>(name, 20, Gender::MALE, "Computer Science", 3.8)); } } // 友元函数 friend void Friend::accessPrivate(PersonManager& pm); }; // 友元函数访问私有成员 void Friend::accessPrivate(PersonManager& pm) { cout << "访问私有成员: people 数量 = " << pm.people.size() << endl; } } // namespace MyNamespace // 预处理器宏定义 #define PRINT_TYPE(var) cout << "类型为: " << typeid(var).name() << endl // 主函数 int main() { using namespace MyNamespace; try { PersonManager pm; // 添加对象 pm.addPerson(make_shared<Student>("张三", 20, Gender::MALE, "计算机科学", 3.9)); pm.addPerson(make_shared<Student>("李四", 22, Gender::FEMALE, "数学", 3.7)); // 使用函数重载 pm.findPerson("张三")->work("C++编程"); // 排序并输出 pm.sortPeopleByAge(); pm.displayAll(); // 文件操作 pm.saveToFile("people.txt"); PersonManager pm2; pm2.loadFromFile("people.txt"); pm2.displayAll(); // 类型转换 Person* p = new Student("王五", 25, Gender::MALE, "AI", 4.0); Student* s = dynamic_cast<Student*>(p); if (s) { s->work("深度学习"); } // 使用宏 PRINT_TYPE(s); delete p; } catch (const exception& e) { cerr << "异常捕获: " << e.what() << endl; } return 0; } #endif // PERSON_H ``` --- ### 🔍 包含的知识点汇总: | 知识点 | 示例位置 | |--------|----------| | 基本语法 | `int`, `string`, `if-else`, `for`, `while` 等 | | 类与对象 | `Person`, `Student` 类定义 | | 构造函数/析构函数 | 构造函数、虚析构函数 | | 封装 | `private`、`protected` 成员 | | 继承 | `Student` 继承 `Person` | | 多态 | 虚函数 `introduce()`、`work()` | | 运算符重载 | 未直接展示,但可扩展 | | this 指针 | `setAge()` 方法中使用 | | 静态成员函数 | `getGenderEnum()` | | 友元类与友元函数 | `Friend` 类访问 `PersonManager` 私有成员 | | 指针与引用 | `Person*`, `shared_ptr<Person>`, `auto` | | 智能指针 | `shared_ptr`, `make_shared` | | new/delete | `new Student(...)`, `delete p` | | STL 容器 | `vector`, `map`, `list` | | 迭代器 | `begin()`, `end()` | | Lambda 表达式 | `sort()` 中使用 Lambda | | 命名空间 | `MyNamespace` | | 异常处理 | `try`, `catch`, `throw` | | 预处理器宏 | `#define PRINT_TYPE(var)` | | 文件操作 | `ifstream`, `ofstream` | | 类型转换 | `dynamic_cast`, `typeid` | --- ### 📌 编译运行方式(g++): ```bash g++ -std=c++17 person_manager.cpp -o person_manager ./person_manager ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值