lalit.kumar.bhasin
BAN USERThe perl way:
my $hash={};
my $arr = [];
while(<STDIN>) {
chomp;
my ($name, $age) = split ',' ;
$hash->{$name} = $age;
}
print sort {$hash->{$a} cmp $hash->{$b} } keys %$hash
Using Priority queue in C++:
#include<string>
#include<iostream>
#include<queue>
#include<cstdlib>
struct Person {
std::string name;
int age;
Person(std::string n , int a):name(n), age(a) {}
};
struct personCompare {
bool operator ()(const Person &p1, const Person &p2) const {
return p1.age > p2.age;
}
};
int main() {
std::priority_queue<Person, std::vector<Person>, personCompare> pq;
std::string line;
while (std::getline(std::cin, line)) {
std::string name = line.substr(0, line.find_first_of(',',0));
int age = std::atoi(line.substr(line.find_first_of(',',0) + 1).c_str());
Person p(name,age);
pq.push(p);
}
while (!pq.empty()) {
Person p = pq.top();
std::cout << p.name << std::endl;
pq.pop();
}
}
C++11:
- lalit.kumar.bhasin July 07, 2015