物件104-3

 avatar
user_6817964
c_cpp
a year ago
860 B
0
Indexable
Never
#include <iostream>
using namespace std;

template <typename x>
void sortpair(x &a, x &b)
{
    if(a > b)
    {
        x c = a;
        a = b;
        b = c;
    }
}

template <typename y>
void sort(y list[6])
{
    for(int i = 0; i < 5; i++)
    {
        for(int j = i+1; j < 6; j++)
        {
            sortpair(list[i], list[j]);
        }
    }
}

template <typename z>
void print(z list[6])
{
    for(int i = 0; i < 6; i++)
    {
        cout << list[i] << " ";
    }
    cout << endl;
}


int main()
{
    int intlist[6] = {3, 5, 2, 7, 1, 9};
    double doublelist[6] = {1.3, 2.4, 7.2, 9, 3.3, 5.1};
    char charlist[6] = {'b', 'u', 'd', 'a', 'w', 'k'};

    sort(intlist);
    sort(doublelist);
    sort(charlist);

    print(intlist);
    print(doublelist);
    print(charlist);

    return 0;
}