C++ 泛型编程(通讯录编排)

题目描述

        ACM集训队最近人气很旺,为了促进队员之间的交流,我们准备制作一个通讯录,每个队员的信息包括:
                tojid 电话 籍贯 出生年-月-日
       各个字段均用英文字符和数字表示。使用空格分隔各个字段,每个字段不包含空格,如:crq的信息为:
                crq 660000 huangyan 1979-1-1
       等到所有队员的资料收集完毕,我们想要编排通讯录以便于队员的查询,编排方式如下:
       首先根据地名作为第一关键字进行字典序排序,由于每年老乡之间都要为队友庆祝生日,因此将生日作为第二关键字进行递增排序。也许你在老乡之间能够找到一个非常有缘的队友即你们的生日是同一天,那么就根据tojid作为第三关键字进行字典序排序,由于tojid是唯一的,因此总能排序。我们确信今后将有大量的新队友加入,因此为了能一劳永逸,请你帮我们编程完成这个任务。

输入

输入数据有n+1行,第一行为集训队队员的总数n (n<=100),下面的n行为每个队员的信息,格式见样例。

输出

输出编排后的结果。

样例输入

5
crq 660000 huangyan 1979-1-1
carter 660000 huangyan 1979-1-1
tzc 668888 linhai 1990-1-11
wk 666666 ningbo 1986-10-1
metoo 665567 jiaojiang 1969-1-13

样例输出

carter 660000 huangyan 1979-1-1
crq 660000 huangyan 1979-1-1
metoo 665567 jiaojiang 1969-1-13
tzc 668888 linhai 1990-1-11
wk 666666 ningbo 1986-10-1
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<set>
using namespace std;
struct Node
{
	string name, tel, addre, birth;
	Node(string name, string tel, string addre, string birth){
		this->name = name;
		this->tel = tel;
		this->addre = addre;
		this->birth = birth;
	}
};
bool operator<(Node a, Node b){
	if (a.addre == b.addre){
		if (a.birth == b.birth){
			return a.name < b.name;
		}
		else{
			return a.birth < b.birth;
		}
	}
	else{
		return a.addre < b.addre;
	}
}
set<Node>Set;
int main()
{

	int n;
	string a[4];
	while (scanf("%d",&n) != EOF){
		for (int i = 0; i < n; i++){
			cin >> a[0] >> a[1] >> a[2] >> a[3];
			Set.insert(Node(a[0], a[1], a[2], a[3]));
		}
		for (set<Node>::iterator it = Set.begin(); it != Set.end(); it++){
			cout << it->name << " " << it->tel << " " << it->addre << " " << it->birth << endl;
		}
		Set.clear();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/A_I_Q/article/details/82905198