首页 > 代码库 > 关于函数模版的字符型数组的操练

关于函数模版的字符型数组的操练

#include<iostream>
using namespace std;

template <typename A, typename A2>
void sortArray(A *array, A2 size)
{
	A2 temp;
	for (A2 i = 0; i < size; i++)
	{
		for (A2 j = i+1; j < size; j++)
		{
			if (array[i] < array[j])
			{ 
				temp = array[i];
				array[i] = array[j];
				array[j] = temp;
			}
		}
	}
}
template <typename A, typename A2>//这里必须单独另加说明,否则导致错误
void Myprint(A *array, A2 size)
{
	for (A2 i = 0; i < size; i++)
	{
		cout << array[i] << "  ";
	}
	cout << endl;
}
int main()
{
	int Myarray[] = { 2,4,6547,21436,435,12,546,1232 };
	int size = sizeof(Myarray) / sizeof(*Myarray);
	sortArray<int, int >(Myarray, size);
	Myprint<int ,int >(Myarray, size);


//	char Mychar[] = { ‘q‘,‘d‘,‘s‘,‘a‘,‘g‘,‘e‘,‘o‘ };
	char Mychar[] = "sjakfjsefdswfwe";//上下这两种的表示方法的区别,为什么第一个会出现乱码
	 int size2 = strlen(Mychar);
	sortArray<char , int >(Mychar, size);
	Myprint<char , int >(Mychar, size);

	cout << "我是漂亮的分割线|\n";
	char Mychar1[] = { ‘q‘,‘d‘,‘s‘,‘a‘,‘g‘,‘e‘,‘o‘ };
	int size3 = sizeof(Mychar1)/sizeof(*Mychar1);
	cout << sizeof(Mychar1) / sizeof(*Mychar1)<<endl;
	sortArray<char, int >(Mychar1, size3);
	Myprint<char, int >(Mychar1, size3);

	system("pause");
}

  

技术分享

 

关于函数模版的字符型数组的操练