template 用法(简单点一下)+送一个逗比并查集

今天学到个有用的东西:大神勿喷啊!!!我乃小白,人嘛,只有积累才能到达巅峰……大笑

因为是刚刚用到,所以就不教的详细了。。怕把你们也教坏了……

这个东西叫做 template ,是一个关键字,至于什么用嘛,先来看看模板库。


相信大家都觉得用模板库很方便:支持各种类型嘛……,要用int就一个<int>,用string 就一个<string>

用的方便之余,我们也在想,要是我们也能编出这么方便的东西就好了。。

好,这就是 template 这个东西的用法,想要深入(别歪了)的可以自己百度。。


引用一段文字::模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计。C++ 的标准库提供许多有用的函数大多结合了模板的观念,如STL以及IO Stream。


大家自己理解。。


下面直接上代码:自己脑洞打开写得一个并查集(这两天做并查集的题……),在附送一道题

实现代码+测试:

#include <iostream>
#include <map>
#include <set>
using namespace std;

template <class T> class BCset {
	#include <map>
	private:
		std::map <T,T> m;
		int size1 = 0;
	public:
		int size() const {
			return size1;
		}
		void into_node (T n) {
			m[n] = n;
			size1++;
		}
		void contact (T n1,T n2) {
			T aa = find (n1);
			T bb = find (n2);
			m [aa] = bb;
		}
		T find (T a) {
			if (m[a] == a)
				return a;
			T ret = find(m[a]);
			m[a] = ret;
			return ret;
		}
		bool same(T n1,T n2)
		{
			T aa = find (n1);
			T bb = find (n2);
			if (aa == bb)
			{
				return true;
			}
			return false;
		}
};


int main () {
	BCset<int> s;
	for (int i = 0; i<10; i++) {
		s.into_node(i);//添加元素 
	}
	cout <<"整数集合测试"<<endl;
	cout <<"元素个数:"<<s.size()<<endl;//元素的个数 
	s.contact(1,2);//让他们之间联系 
	cout <<"将2和3连起来"<<endl;
	s.contact(1,3);//让他们之间联系 
	cout <<"将1和3连起来"<<endl;
	cout <<"查找2与4: "<<s.same(2,4)<<endl;//查找 
	cout <<"查找2与3: "<<s.same(2,3)<<endl;//查找 
	cout <<"查找1与3: "<<s.same(1,3)<<endl;//查找 
	
	cout <<endl<<"字符串集合测试"<<endl;
	string sr;
	BCset<string> st_s;
	sr = "小明";
	cout <<sr<<"已入集"<<endl; 
	st_s.into_node(sr);
	sr = "小红";
	cout <<sr<<"已入集"<<endl; 
	st_s.into_node(sr);
	sr = "绿帽";
	cout <<sr<<"已入集"<<endl; 
	st_s.into_node(sr);
	sr = "大黄";
	cout <<sr<<"已入集"<<endl<<endl; 
	st_s.into_node(sr);
	
	st_s.contact("小明","小红");
	cout <<"将绿帽和大黄连起来"<<endl;
	st_s.contact("绿帽","大黄");
	cout <<"将小明和小红连起来"<<endl;
	cout <<"查找 小明 与 小红: "<<st_s.same("小明","小红")<<endl;//查找 
	cout <<"查找 小明 与 大黄: "<<st_s.same("小明","大黄")<<endl;//查找 
	st_s.contact("小明","大黄");
	cout <<"将小明和大黄连起来"<<endl;
	cout <<"查找 小明 与 大黄: "<<st_s.same("小明","大黄")<<endl;//查找 
	 
}

再来道题


【一本通】家谱(gen)
Description
现代的人对于本家族血统越来越感兴趣,现在给出充足的父子关系,请你编写程序找到某个人的最早的祖先。


Input
输入由多行组成,首先是一系列有关父子关系的描述,其中每一组父子关系由二行组成,用#name的形式描写一组父子关系中的父亲的名字,用+name的形式描写一组父子关系中的儿子的名字; 
接下来用?name的形式表示要求该人的最早的祖先; 
最后用单独的一个$表示文件结束。 
规定每个人的名字都有且只有6个字符,而且首字母大写,且没有任意两个人的名字相同。最多可能有1000组父子关系,总人数最多可能达到50000人,家谱中的记载不超过30代。


Output
按照输入数据的要求顺序,求出每一个要找祖先的人的祖先,格式:本人的名字+一个空格+祖先的名字+回车。


Sample Input
#George 
+Rodney 
#Arthur 
+Gareth 
+Walter 
#Gareth 
+Edward 
?Edward 
?Walter 
?Rodney 
?Arthur 
$


Sample Output
Sample Output
Edward Arthur 
Walter Arthur 
Rodney George 
Arthur Arthur


家谱问题,并查集!

#include <iostream>
#include <map>
using namespace std;
template <class T> class BCset {
	private:
		map <T,T> m;
		map <T,bool> chong;
		int size1;
	public:
		BCset ()
		{
			size1 = 0;
		}
		int size() const {
			return size1;
		}
		void into_node (T n) {
			if (!chong[n]) {
				m[n] = n;
				size1++;
				chong[n] = 1;
			}
		}
		void contact (T n1,T n2) {
			T aa = find (n1);
			T bb = find (n2);
			m [aa] = bb;
		}
		T find (T a) {
			if (m[a] == a)
				return a;
			T ret = find(m[a]);
			m[a] = ret;
			return ret;
		}
		bool same(T n1,T n2) {
			T aa = find (n1);
			T bb = find (n2);
			if (aa == bb) {
				return true;
			}
			return false;
		}
};
int main () {
	BCset <string> bcs;
	string sr,one,two;
	while (sr != "$") {
		cin >>sr;
		if (sr == "$")
			break;
		if (sr[0]=='#') {
			one = sr.substr(1,sr.size()-1);
			bcs.into_node(one);
		}
		if (sr[0]=='+') {
			two = sr.substr(1,sr.size()-1);
			bcs.into_node(two);
			bcs.contact(two,one);
		}
		if (sr[0]=='?')
		{
			cout <<sr.substr(1,sr.size()-1)<<" "<<bcs.find(sr.substr(1,sr.size()-1))<<endl;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值