李太白年龄:80 加入列表后,最小的年龄现在是:80
赵四天年龄:70 加入列表后,最小的年龄现在是:70
王天霸年龄:90 加入列表后,最小的年龄现在是:70
毛强年龄:20 加入列表后,最小的年龄现在是:20
周瑜年龄:30 加入列表后,最小的年龄现在是:20
万茜年龄:40 加入列表后,最小的年龄现在是:20
20
万茜离开列表后,最小的年龄现在是:20
周瑜离开列表后,最小的年龄现在是:20
毛强离开列表后,最小的年龄现在是:70
王天霸离开列表后,最小的年龄现在是:70
赵四天离开列表后,最小的年龄现在是:80
李太白离开列表后,最小的年龄现在是:150
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class People { //人类private:string name;int age;public:static int min;static vector<People*> v;People(string n, int a) : name(n), age(a) {People::min = age < min ? age : min;People::v.push_back(this);cout << name<<"年龄:"<<age<< " 加入列表后,最小的年龄现在是:"<<People::min << endl;}~People() {for (int i = 0; i < People::v.size(); i++) {if (People::v[i] && this == People::v[i]) {People::v[i] = NULL;break;}}min = 150;for (vector<People*>::iterator i = People::v.begin();i != People::v.end(); i++) {if (*i && (*i)->age < People::min)People::min = (*i)->age;}cout << name<< "离开列表后,最小的年龄现在是:"<<People::min << endl;}
};
int People::min = 150;
vector<People*> People::v;
int main() {People p1("李太白", 80);People p2("赵四天", 70);People p3("王天霸", 90);People p4("毛强", 20);People p5("周瑜", 30);People p6("万茜", 40);cout << People::min << endl;return 0;
}