stl第一部分: string, vector, deque, stack
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
// stack 没有迭代器:不支持遍历
|
||||
// 原因: 栈是一种特殊的容器,只能在栈顶进行插入和删除操作
|
||||
|
||||
// stack 常用接口
|
||||
// 1. push(elem); // 入栈
|
||||
// 2. pop(); // 出栈
|
||||
// 3. top(); // 返回栈顶元素
|
||||
// 4. empty(); // 判断栈是否为空
|
||||
// 5. size(); // 返回栈中元素的个数
|
||||
// 6. swap(stk1, stk2); // 交换两个栈
|
||||
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
stack<int> stk;
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
stk.push(i);
|
||||
}
|
||||
|
||||
cout << "当前栈中元素个数:" << stk.size() << endl;
|
||||
cout << "栈顶的元素: " << stk.top() << endl;
|
||||
|
||||
// 只弹出 600 这个元素
|
||||
// 需要一个临时变量来保存弹出的元素
|
||||
stack<int> tmp_stk;
|
||||
while (stk.top() != 600)
|
||||
{
|
||||
tmp_stk.push(stk.top());
|
||||
stk.pop();
|
||||
}
|
||||
cout << " ----------\n弹出 600 前的栈顶元素: "
|
||||
<< stk.top() << endl;
|
||||
stk.pop();
|
||||
cout << "弹出 600 后的栈顶元素: "
|
||||
<< stk.top()
|
||||
<< endl
|
||||
<< " ----------\n";
|
||||
while (!tmp_stk.empty())
|
||||
{
|
||||
stk.push(tmp_stk.top());
|
||||
tmp_stk.pop();
|
||||
}
|
||||
cout << "当前栈中元素个数:" << stk.size() << endl;
|
||||
cout << "栈顶的元素: " << stk.top() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Date
|
||||
{
|
||||
// 重载 << 以显示日期
|
||||
friend ostream &operator<<(ostream &out, const Date &date)
|
||||
{
|
||||
out << date.year << "年" << date.month << "月" << date.day << "日";
|
||||
return out;
|
||||
}
|
||||
|
||||
private:
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
|
||||
public:
|
||||
Date(int y, int m, int d) : year(y), month(m), day(d) {}
|
||||
|
||||
public:
|
||||
bool operator<(const Date &date) const
|
||||
{
|
||||
// if (year < date.year)
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// else if (year == date.year)
|
||||
// {
|
||||
// if (month < date.month)
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// else if (month == date.month)
|
||||
// {
|
||||
// if (day < date.day)
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
return year < date.year ||
|
||||
(year == date.year && month < date.month) ||
|
||||
(year == date.year && month == date.month && day < date.day);
|
||||
}
|
||||
bool operator>(const Date &date) const
|
||||
{
|
||||
return year > date.year && month > date.month && day > date.day;
|
||||
}
|
||||
};
|
||||
|
||||
class Worker
|
||||
{
|
||||
friend Worker &operator=(const Worker &other);
|
||||
// {
|
||||
// if (this != &other)
|
||||
// {
|
||||
// name = other.name;
|
||||
// hire_date = other.hire_date;
|
||||
// volume = other.volume;
|
||||
// }
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
private:
|
||||
string name; // 姓名
|
||||
Date hire_date; // 入职日期
|
||||
double volume; // 业绩
|
||||
|
||||
public:
|
||||
Worker(const string &name, const Date &hire_date, double volume)
|
||||
: name(name), hire_date(hire_date), volume(volume) {}
|
||||
Worker(const Worker &other)
|
||||
{
|
||||
name = other.name;
|
||||
hire_date = other.hire_date;
|
||||
volume = other.volume;
|
||||
}
|
||||
~Worker()
|
||||
{
|
||||
cout << "析构函数被调用" << endl;
|
||||
}
|
||||
|
||||
public:
|
||||
void show()
|
||||
{
|
||||
cout << "name = " << name << ", hire_date = " << hire_date << ", volume = " << volume << endl;
|
||||
}
|
||||
};
|
||||
|
||||
Worker &operator=(const Worker &other)
|
||||
{
|
||||
name = other.name;
|
||||
hire_date = other.hire_date;
|
||||
volume = other.volume;
|
||||
}
|
||||
|
||||
void sortByHireDate(Worker *p, int size)
|
||||
{
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
int min = i;
|
||||
for (int j = i + 1; j < size; i++)
|
||||
{
|
||||
if (p[min].hire_date > p[j].hire_date)
|
||||
{
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
if (min != i)
|
||||
{
|
||||
swap(p[min], p[i]);
|
||||
// Worker tmp = p[i]; // 拷贝构造
|
||||
// p[i] = p[min]; // operator= 重载
|
||||
// p[min] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void sortByVolume(Worker *p, int size)
|
||||
{
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Worker w1("disen", Date(2020, 1, 15), 1000);
|
||||
Worker w2("zhangsan", Date(2020, 1, 16), 200);
|
||||
Worker w3("lisi", Date(2020, 2, 17), 400);
|
||||
Worker w4("wangwu", Date(2020, 2, 18), 900);
|
||||
Worker w5("zhaoliu", Date(2020, 2, 18), 100);
|
||||
|
||||
Worker wrks[] = {w1, w2, w3, w4, w5};
|
||||
|
||||
sortByHireDate(wrks, 5); // 先排序
|
||||
|
||||
// vector<Worker> vec;
|
||||
// vec.assign(wrks, wrks + 5);
|
||||
|
||||
w1.show();
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
class Worker;
|
||||
class Date
|
||||
{
|
||||
friend Worker;
|
||||
friend ostream &operator<<(ostream &cout, Date &date)
|
||||
{
|
||||
cout << date.year << "年" << date.month << "月" << date.day << "日";
|
||||
return cout;
|
||||
}
|
||||
|
||||
private:
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
|
||||
public:
|
||||
Date(int year, int month, int day)
|
||||
{
|
||||
this->year = year;
|
||||
this->month = month;
|
||||
this->day = day;
|
||||
}
|
||||
bool operator>(Date &other)
|
||||
{
|
||||
// cout << *this << " pk " << other << endl;
|
||||
return year > other.year || (year == other.year && month > other.month) || (year == other.year && month == other.month && day > other.day);
|
||||
}
|
||||
bool operator<=(Date &other)
|
||||
{
|
||||
return year <= other.year && month <= other.month && day <= other.day;
|
||||
}
|
||||
};
|
||||
class Worker
|
||||
{
|
||||
public:
|
||||
string name; // 姓名
|
||||
Date *hire_date; // 入职时间
|
||||
double volume; // 业绩值
|
||||
|
||||
public:
|
||||
Worker(const string &name, Date *hire_date, double volume)
|
||||
{
|
||||
this->name = name;
|
||||
this->hire_date = hire_date;
|
||||
this->volume = volume;
|
||||
}
|
||||
Worker(const Worker &other)
|
||||
{
|
||||
Date *hire_date = new Date(other.hire_date->year, other.hire_date->month, other.hire_date->day);
|
||||
this->hire_date = hire_date;
|
||||
this->name = other.name;
|
||||
this->volume = other.volume;
|
||||
}
|
||||
Worker &operator=(const Worker &other)
|
||||
{
|
||||
this->hire_date->year = other.hire_date->year;
|
||||
this->hire_date->month = other.hire_date->month;
|
||||
this->hire_date->day = other.hire_date->day;
|
||||
this->name = other.name;
|
||||
this->volume = other.volume;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Worker()
|
||||
{
|
||||
if (hire_date)
|
||||
delete hire_date;
|
||||
hire_date = NULL;
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
cout << "name=" << name << ",hire_date=" << *hire_date << ",volume=" << volume << endl;
|
||||
}
|
||||
};
|
||||
|
||||
void sortByHireDate(Worker *p, int size)
|
||||
{
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
{
|
||||
int min = i;
|
||||
for (int j = i + 1; j < size; j++)
|
||||
{
|
||||
if (*(p[min].hire_date) > *(p[j].hire_date))
|
||||
{
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
if (min != i)
|
||||
{
|
||||
// cout << p[i].name << " 和 " << p[min].name << " 交换数据" << endl;
|
||||
Worker tmp = p[i]; // 拷贝构造函数
|
||||
p[i] = p[min]; // operator=重载
|
||||
p[min] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sortByVolume(Worker *p, int size)
|
||||
{
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
{
|
||||
int min = i;
|
||||
for (int j = i + 1; j < size; j++)
|
||||
{
|
||||
if (p[min].volume < p[j].volume)
|
||||
{
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
if (min != i)
|
||||
{
|
||||
// cout << p[i].name << " 和 " << p[min].name << " 交换数据" << endl;
|
||||
Worker tmp = p[i]; // 拷贝构造函数
|
||||
p[i] = p[min]; // operator=重载
|
||||
p[min] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
Worker w1("disen", new Date(2020, 1, 15), 1000);
|
||||
Worker w2("jack", new Date(2021, 1, 15), 100);
|
||||
Worker w3("lucy", new Date(2020, 2, 15), 500);
|
||||
Worker w4("mack", new Date(2020, 3, 25), 300);
|
||||
Worker w5("judy", new Date(2022, 5, 18), 400);
|
||||
Worker w6("rose", new Date(2023, 1, 10), 1200);
|
||||
|
||||
Worker m[] = {w1, w2, w3, w4, w5, w6};
|
||||
// sortByHireDate(m, 6);
|
||||
sortByVolume(m, 6);
|
||||
vector<Worker> v1(m, m + 6);
|
||||
stack<Worker> s1;
|
||||
vector<Worker>::iterator it = v1.begin();
|
||||
while (it != v1.end())
|
||||
{
|
||||
(*it).show();
|
||||
s1.push(*it);
|
||||
it++;
|
||||
}
|
||||
|
||||
// 辞退2位新来的员工
|
||||
// cout << "-----辞退2位新来的员工----" << endl;
|
||||
cout << "-----业绩最差的2位员工----" << endl;
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
s1.top().show();
|
||||
s1.pop();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user