棧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插入和刪除
map查找和統計
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;
}