首页 > 代码库 > The study for the template
The study for the template
/*冒泡排序.cpp---study for the template*/
#include<iostream>
using namespace std;
template <typename ElementType>//关键
/*ElementType max(ElementType a, ElementType b){
return a > b ? a : b;
}*/
void SortBuble(ElementType* a,int size){//from little to big;
int work = 0;
ElementType temp;
for (int i = 0; i < size; i++){
for (int j = size - 1; j>=i; j--){
if (a[j] < a[j - 1]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
work = 1;
}
}
if (work == 0) break;
}
}
int main(){
cout.setf(ios_base::showpoint);
cout.precision(3);
/*cout << "max=" << max(5.0, 6.0)<<endl;
cout << "max=" << max(5, 6) << endl;
cout << "max=" << max(‘a‘, ‘b‘) << endl;*/
int a = 3;//不用中间量交换两值
int b = 4;
cout << "a=" << a << "b=" << b << endl;
a = a - b;
b = a + b;
a = b - a;
cout << "a=" << a << "b=" << b << endl;
double s[10] = { 2.2, 3.5, 5.2, 6.25, 4.2, 8, 7.7, 2.8, 1, 15 };
SortBuble(s, 10);
for (int i = 0; i < 10; i++){ cout << s[i] << endl; }
system("pause");
return 0;
}
The study for the template