Untitled
unknown
plain_text
2 years ago
2.3 kB
11
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class Person{
private:
string name;
int age;
int numHobbies;
string *hobbies;
public:
Person();
Person(string, int, int, string *);
~Person();
void displayInfo();
string getName() {
return name;
};
int getAge(){
return age;
};
int getNumHobbies(){
return numHobbies;
};
};
Person::Person(){
name = "";
age = 0;
numHobbies = 0;
hobbies = nullptr;
}
Person::Person(string n, int a, int numH, string *h){
name = n;
age = a;
numHobbies = numH;
hobbies = h;
}
void Person::displayInfo(){
cout << "Name: " << name << ", Age: " << age << ", Number of Hobbies " << numHobbies << endl;
for(int i = 0; i < numHobbies; i++){
if(i != 0) cout << ", ";
cout << hobbies[i];
//hobbies++;
}
cout << endl;
}
bool cmp1(Person a, Person b){
if(a.getNumHobbies() == b.getNumHobbies()){
if(a.getAge() == b.getAge()){
return b.getName() > a.getName();
}
return a.getAge() > b.getAge();
}
return a.getNumHobbies() > b.getNumHobbies();
}
int main(){
int n;
cin >> n;
vector<Person> persons(n);
for (int i = 0; i < n; ++i){
string name;
int age, numHobbies;
cin >> name >> age >> numHobbies;
string *hobbies = new string[numHobbies];
for (int j = 0; j < numHobbies; ++j){
cin >> hobbies[j];
}
Person temp(name, age, numHobbies, hobbies);
persons[i] = temp;
delete[] hobbies;
}
sort(persons.begin(), persons.end(), cmp1);
for (int i = 0; i < n; ++i){
cout << "Person " << i + 1 << " info:" << endl;
persons[i].displayInfo();
}
return 0;
}Editor is loading...
Leave a Comment