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;

}



本文转自 神迹难觅 51CTO博客,原文链接:http://blog.51cto.com/ji123/1917866,如需转载请自行联系原作者

相关文章
|
27天前
|
Java 编译器 Maven
Java“class file contains wrong class”解决
当Java程序运行时出现“class file contains wrong class”错误,通常是因为类文件与预期的类名不匹配。解决方法包括:1. 确保类名和文件名一致;2. 清理并重新编译项目;3. 检查包声明是否正确。
|
7月前
<template> 上的 v-for​
<template> 上的 v-for​
|
7月前
|
Linux
报错 Package ‘oniguruma‘, required by ‘virtual:world‘, not found
报错 Package ‘oniguruma‘, required by ‘virtual:world‘, not found
379 0
|
7月前
|
编译器 测试技术 调度
C++ 中 template<class T>和template<typename T>的区别
C++ 中 template<class T>和template<typename T>的区别
173 0
|
JavaScript
Vue中遇到的Bug( Component name “School“ should always be multi-word vue/multi-word-component-names)
Vue中遇到的Bug( Component name “School“ should always be multi-word vue/multi-word-component-names)
126 0
|
JavaScript 前端开发
成功解决Component template should contain exactly one root element
成功解决Component template should contain exactly one root element
|
Java
Java Error: The public type HelloWorld must be defined in its own file
Java Error: The public type HelloWorld must be defined in its own file
167 0
【Junit 报错】Test class should have exactly one public zero-argument constructor和Test class can only have one constructor
错误1: 1 java.lang.Exception: Test class should have exactly one public zero-argument constructor 2 at org.
6160 0
|
PHP
【PHP】Cannot declare class app\admin\controller\Goods because the name is already in use
【PHP】Cannot declare class app\admin\controller\Goods because the name is already in use
255 0
【PHP】Cannot declare class app\admin\controller\Goods because the name is already in use