码迷,mamicode.com
首页 > 其他好文 > 详细

The study for the template

时间:2017-04-20 21:51:47      阅读:146      评论:0      收藏:0      [点我收藏+]

标签: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

标签:template

原文地址:http://ji123.blog.51cto.com/11333309/1917866

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!