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]) 这一句有问题。。。。。
这是为什么??
一个能通过。一个不能通过
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]) 这一句有问题。。。。。
这是为什么??
一个能通过。一个不能通过
相关文章推荐
- C++源码分享(四):对象属性泛型基类
- 泛型和面向对象C++
- C++模板实现泛型链表
- [转]Anders Hejlsberg谈C#、Java和C++中的泛型
- C++ 模板和 C# 泛型之间的区别
- C++ 学习笔记(10)泛型算法、lambda表达式、bind函数、迭代器
- C++泛型 PK JAVA泛型
- C++ 泛型算法unique 和unique_copy :删除相邻的重复元素
- 十种排序算法的C++泛型实现
- C++泛型中replace和replace_copy
- 泛型在C++中的应用
- C++学习笔记25——泛型算法之写入容器
- 由C++的泛型句柄类思考OpenCV的Ptr模板类
- C++ 泛型基础
- C++模板实现泛型队列
- [ZT]Anders Hejlsberg谈C#、Java和C++中的泛型
- C++“模板”和Java“泛型”
- C++ 学习笔记:原生指针 && 泛型指针 && 智能指针
- C++ 泛型
- Java 中的泛型与 C++ 模板的比较