题目一:
#include <iostream>
#include <queue>
#include <functional>
using namespace std;
enum Sex
{
Women = 1, Men = 0,
};
struct Person
{
int TrueAge;
char TrueSex;
Sex sex;
Person(int TrueAge, char TrueSex)
{
this->TrueAge = TrueAge;
this->TrueSex = TrueSex;
sex = TrueSex == 'f' ? Women : Men;
}
bool operator < (const Person& person1) const // 此时的this指针为指向常量的指针常量(const Person const*)
{
int Age1 = this->TrueAge >= 60 ? this->TrueAge : 20;
int Age2 = person1.TrueAge >= 60 ? person1.TrueAge : 20;
// 我一般排列数组内的元素喜欢用权重,把各部分给予相应的权重,最终以求和后的数值来排序
return (Age1 + this->sex) < (person1.sex + Age2);
}
friend ostream& operator << (ostream& Output, const Person& Person1);
};
ostream& operator << (ostream& Output, const Person& Person1)
{
Output << Person1.TrueAge << " " << Person1.TrueSex; // 这里千万不要用endl清空缓存区
return Output;
}
int main()
{
Person Person1(63, 'm'), Person2(32, 'f'), Person3(82, 'f');
priority_queue<Person, vector<Person>, less<Person>> PriorQueueArray;
PriorQueueArray.push(Person1);
PriorQueueArray.push(Person2);
PriorQueueArray.push(Person3);
while (!PriorQueueArray.empty())
{
cout << PriorQueueArray.top() << endl;
PriorQueueArray.pop();
}
}
题目二:
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main()
{
stack<string, vector<string>> StackArray;
StackArray.push("张三");
StackArray.push("李四");
StackArray.push("王五");
while (!StackArray.empty())
{
cout << StackArray.top() << endl;
StackArray.pop();
}
}