当前位置: 首页 > news >正文

第十五章 STL(stack、queue、list、set、map容器使用)

栈stack

使用:

队列queue

双向循环链表list

list构造函数

list 赋值和交换

list 大小操作

list 插入和删除

list 数据存取

list 反转和排序

排序算法sort降序操作

排序案例

#include<iostream>
using namespace std;
#include<list>class Person {
private:string name;int age;int hight;public:Person(string n, int a, int h) {this->name = n;this->age = a;this->hight = h;}void show() const{cout << "姓名:" << this->name << " " << "年龄:" << this->age << " " << "身高:" << this->hight << endl;}string getName() const {return name;}int getAge() const {return age;}int getHight() const {return hight;}};bool comparePerson(const Person& p1, const Person& p2) {if (p1.getAge() < p2.getAge())				//年龄升序return true;else if (p1.getAge() == p2.getAge()) {	if (p1.getHight() > p2.getHight())		//身高降序return true;}return false;
}void printPerson(const list<Person> &L) {for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++) {it->show();}
}int main() {list<Person> persons;Person p1("张三", 20, 160);Person p2("李四", 18, 180);Person p3("王五", 8, 180);Person p4("赵六", 18, 150);//插入数据persons.push_back(p1);persons.push_back(p2);persons.push_back(p3);persons.push_back(p4);cout << "排序前" << endl;printPerson(persons);persons.sort(comparePerson);cout << "排序后" << endl;printPerson(persons);}

集合set(排序树)

set构造和赋值

set大小和交换

set插入和删除

set查找和统计

set和multiset区别

pair对组

set容器排序

set存放内置数据类型

set存放自定义数据类型

字典map

map构造和赋值

map大小和交换

map插入和删除

set查找和统计

map和multimap区别

同set

map容器排序

练习

#include <iostream>
using namespace std;
#include<vector>
#include<map>class employee {
private:string name;int salary;public:employee(string name, int salary) {this->name = name;this->salary = salary;}string getName() {return name;}int getSalary() {return salary;}
};void createEmployee(vector<employee> &e) {string nameSeed = "ABCDEFGHIJ";string name;int salary = rand() % 10000 + 10000;for (int i = 0; i < 10; i++) {name = "员工";name += nameSeed[i];e.push_back(employee(name, salary));salary = rand() % 10000 + 10000;}}void setGroups(multimap<int, employee> &G,vector<employee> &E) {std::srand(std::time(0));int depId;for(vector<employee>::iterator it = E.begin(); it!= E.end(); it++) {depId = rand() % 3 + 1; // 随机分配部门ID 1-3G.insert(make_pair(depId, *it)); // 将员工分配到对应部门}}int main() {multimap<int, employee> groups;vector<employee> e;			//员工createEmployee(e);			//员工初始化setGroups(groups, e);multimap<int, employee>::iterator pos = groups.find(1);		//策划部门int count = groups.count(1);								//统计部门人数int index = 0;cout << "策划部门员工信息:" << endl;for (; pos != groups.end() && index < count; pos++,index++) {cout << "姓名:" << pos->second.getName() << " 工资:" << pos->second.getSalary() << endl;}cout << "----------------------------------------" << endl;pos = groups.find(2);		//美术部门count = groups.count(2);								//统计部门人数index = 0;cout << "美术部门员工信息:" << endl;for (; pos != groups.end() && index < count; pos++, index++) {cout << "姓名:" << pos->second.getName() << " 工资:" << pos->second.getSalary() << endl;}cout << "----------------------------------------" << endl;pos = groups.find(3);		//研发部门count = groups.count(3);								//统计部门人数index = 0;cout << "研发部门员工信息:" << endl;for (; pos != groups.end() && index < count; pos++, index++) {cout << "姓名:" << pos->second.getName() << " 工资:" << pos->second.getSalary() << endl;}system("pause");return 0;
}

http://www.dtcms.com/a/273462.html

相关文章:

  • 如何将 iPhone 文件传到 Mac?
  • C++11中的std::minmax与std::minmax_element:原理解析与实战
  • macOS 笔记本下 Gemini CLI 客户端网络连接问题诊断与解决方案
  • Android开发封装防抖xxx秒操作
  • 莫兰迪色系工作总结汇报PPT模版分享
  • Java 中使用 Stream 将 List 转换为 Map 实战笔记(生产级版)
  • 用 React Three Fiber 实现 3D 城市模型的扩散光圈特效
  • Android 开发中插桩
  • RK3566/RK3568 Android11 修改selinux模式
  • JAVA后端开发——类命名规范
  • android 的软件盘
  • 五镜头倾斜摄影相机的技术优势与应用原理
  • 影石(insta360)GO3拇指相机格式化后的恢复方法
  • 远程登录docker执行shell报错input is not a terminal问题
  • 宝塔命令Composer 更改数据源不生效
  • php中调用对象的方法可以使用array($object, ‘methodName‘)?
  • BP神经网络对时序数据进行分类
  • SpringBoot项目保证接口幂等的五种方法!
  • Typecho性能优化全攻略:从数据库到PHP的深度调优
  • 线性回归原理推导与应用(十):逻辑回归多分类实战
  • 文章发布易优CMS(Eyoucms)网站技巧
  • 1Panel V1 无缝升级到 V2 版本 实现多个 PHP 网站共享一个容器
  • 搭建渗透测试环境
  • 【工具变量】全国省市区县土地出让结果公告数据(2000-2024年)
  • 【操作系统】线程
  • RabbitMQ 之消息积压
  • 从 Python 演进探寻 AI 与云对编程语言的推动
  • 【C/C++ shared_ptr 和 unique_ptr可以互换吗?】
  • 传输层协议UDP原理
  • 秋招小白学数据结构-1-数据结构前置知识