Csdn论坛关于一个模板特化不能执行的问题的修改

好吧,现学现卖,表示标题有点长啊。

问题不在描述了,粘贴格式不对。

只把我修改好的特化版本代码贴出:

#include <string>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

template <typename Type> 
int count(Type *a, int sz, Type which )
{
	int n = 0;
	for(int i=0; i < sz; i++)
		if ( a[i] == which )
			n++;
	return n;
}

template<>//这里也要写法要注意!
int count<string>(string* a,int sz,string which)
{
	int n = 0;
	for(int i=0; i < sz; i++)
		if ( a[i] == which )
			n++;
	return n;
}

int main()
{
	int a[10] = {1,5,6,9,2,123,5,78,54,5};
	double b[6] = {1.0,2.0,3.0,2.0,6.0,2.0};
	string s[6] = {"aa","bb","aa","cc","dd","aa",};

	cout << "the number of 5 is: " << count(a,10,5) << endl;
	cout << "the number of 2.0 is: " << count(b,6,2.0) <<endl;

	cout << "the number of string \"aa\" is: " << count(s,6,string("aa")) << endl;

	system("pause");
	return 0;
}

  

 

posted @ 2012-09-30 22:06  hailong  阅读(174)  评论(0编辑  收藏  举报