栈stack


使用:


队列queue




双向循环链表list

list构造函数



list 赋值和交换


list 大小操作


list 插入和删除


list 数据存取


list 反转和排序


排序算法sort降序操作

排序案例

cpp
#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容器排序


练习

cpp
#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-3
G.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;
}
