BJFU2023-C++程序设计-实验5-模板

这篇博客介绍了C++模板编程的应用,包括Swap函数实现不同类型数据交换,SortFunctionTemplate用于数组排序,Search函数在数组中查找元素,TVector3模板类构建3D向量,以及StackClassTemplate实现模板化的堆栈类。同时,还提供了选做题目MyQueue的类模板设计。
摘要由CSDN通过智能技术生成

Swap

描述

用模板函数Swap实现对不同类型的数据进行交换。

并使用如下主函数测试。

int main()
{
    int a1, a2;
    std::cin >> a1 >> a2;
    Swap(a1, a2);
    std::cout << a1 << "," << a2 << std::endl;

    double b1, b2;
    std::cin >> b1 >> b2;
    Swap(b1, b2);
    std::cout << b1 << "," << b2 << std::endl;

    char c1, c2;
    std::cin >> c1 >> c2;
    Swap(c1, c2);
    std::cout << c1 << "," << c2 << std::endl;

    return 0;
}

注意,本题只需要提交Swap函数代码,头文件和main函数系统已经提供。

输入

输入共三行。第一行两整数,第二行两浮点数,第三行两字符

输出

输出共三行。每一行为对应输入处理后的结果,输出的两个数用逗号隔开

#include<iostream>
using namespace std;

template<typename T>
void Swap(T& a, T& b) {
    T temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a1, a2;
    std::cin >> a1 >> a2;
    Swap(a1, a2);
    std::cout << a1 << "," << a2 << std::endl;

    double b1, b2;
    std::cin >> b1 >> b2;
    Swap(b1, b2);
    std::cout << b1 << "," << b2 << std::endl;

    char c1, c2;
    std::cin >> c1 >> c2;
    Swap(c1, c2);
    std::cout << c1 << "," << c2 << std::endl;

    return 0;
}

SortFunctionTemplate

描述

用模板函数实现数组的输入、排序和输出。并使用如下主函数测试你的模板

int main()
{
    const int LEN = 5;
    int type;
    while (std::cin >> type)
    {
        switch (type) 
        {
            case 0: 
            {
                int a1[LEN];
                Input<int>(a1, LEN); Sort<int>(a1, LEN); Output<int>(a1, LEN);
                break;
            }
            case 1: 
            {
                char a2[LEN];
                Input(a2, LEN); Sort(a2, LEN); Output(a2, LEN); 
                break; 
            }
            case 2: 
            {
                double a3[LEN];
                Input(a3, LEN); Sort(a3, LEN); Output(a3, LEN);
                break;
            }
        }
    }

    return 0;
}

注意:本题只提交Input,Sort, Output函数代码。其余部分系统已包含。

输入

输入包含多组测试数据。每组数据为两行,第一行整数type(0、1、2)。第二行为相应数组的5个元素。

输出

#include<iostream>
using namespace std;

template<typename T>
void Input(T arr[], int len) {
    for (int i = 0; i < len; i++) {
        cin >> arr[i];
    }
}

template<typename T>
void Sort(T arr[], int len) {
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len; j++) {
            if (arr[i] > arr[j]) {
                T temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

template<typename T>
void Output(T arr[], int len) {
    for (int i = 0; i < len-1; i++) {
        cout << arr[i] << ", ";
    }
    cout << arr[len - 1] << endl;
}

int main()
{
    const int LEN = 5;
    int type;
    while (std::cin >> type)
    {
        switch (type)
        {
        case 0:
        {
            int a1[LEN];
            Input<int>(a1, LEN); Sort<int>(a1, LEN); Output<int>(a1, LEN);
            break;
        }
        case 1:
        {
            char a2[LEN];
            Input(a2, LEN); Sort(a2, LEN); Output(a2, LEN);
            break;
        }
        case 2:
        {
            double a3[LEN];
            Input(a3, LEN); Sort(a3, LEN); Output(a3, LEN);
            break;
        }
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值