您的位置:首页 > 编程语言 > C语言/C++

c++中的泛型

2011-07-18 08:51 155 查看
#include<iostream>

using std::cout;
using std::endl;

template<class T>class Test
{
//冒泡排序
public :static void BubbleSort(T a[], int n)
{
int i, j;

T temp;

bool flag=false;

for(i=n-1;i>=0;i--)
{
flag=false;

for(j=0;j<i;j++)
{

if (a[j + 1]<a[j])

{
temp=a[j+1]; a[j+1]=a[j]; a[j]=temp;

flag=true;
}

}

if(flag==false) break;

}

} //end BubbleSort

}; //end Test

int main()
{
int i;

int a[5]={23,45,8,0,9};

Test<int>::BubbleSort(a,5);

for(i=0;i<5;i++)std::cout<<a[i]<<std::endl;

system ("pause");

return 0;

}

代码是完全可以通过, 不用所谓接口,但是到了C#中,是无法通过的,提示: if (a[j + 1]<a[j]) 这一句有问题。。。。。

这是为什么??

一个能通过。一个不能通过
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: