C++类模板

类模板是C++中的一种特殊类型
它允许我们定义一种通用的数据类型,而不需要指定实际使用的数据类型,这种数据类型可以用于任何数据类型。

类模板允许我们写出一个类的通用定义,该类可以使用任何数据类型作为其数据成员的类型。这样,我们就可以将一个类实例化为多个具有不同数据类型的对象,而不需要为每个对象编写不同的类定义

类模板的一个重要作用是提高代码的重用性和可维护性。当我们需要处理不同类型的数据时,可以通过使用类模板来避免编写大量的类定义。另外,使用类模板还可以使代码更加灵活和通用,可以在不同的应用程序中重复使用

以下是一个使用类模板实现一个通用的栈类的例子:

#include <iostream>

using namespace std;
// 定义一个类模板Stack,它可以接受任何类型T的数据
template<typename T>
class Stack {
    private:
    // 堆栈的底层存储使用数组,T表示存储的数据类型
        T *stackArray;
    // 栈的大小
        int stackSize;
        int top;

    public:
    // 构造函数,初始化栈的大小和底层数组
        Stack(int size) {
            stackSize = size;
            stackArray = new T[stackSize];
            top = -1;
        }
	// 析构函数,释放堆内存
        ~Stack() {
            delete [] stackArray;
        }
	 // 将元素压入栈中,如果栈已满则打印错误信息
        void push(T value) {
            if (top == stackSize - 1) {
                cout << "Stack is full!\n";
            } else {
                top++;
                stackArray[top] = value;
            }
        }
		// 从栈中弹出一个元素,如果栈为空则打印错误信息
        T pop() {
            if (top == -1) {
                cout << "Stack is empty!\n";
                 // 在这里返回了-1,实际上这样并不合适,因为-1并不是所有类型的默认值
            // 可以使用std::optional<T>类型,这样可以避免返回一个无意义的值
                return -1;
            } else {
                // 弹出栈顶元素并返回
                T value = stackArray[top];
                top--;
                return value;
            }
        }
};

int main() {
    Stack<int> intStack(5);

    intStack.push(10);
    intStack.push(20);
    intStack.push(30);
    intStack.push(40);
    intStack.push(50);

    cout << "Popping values from intStack:\n";
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;
// 创建一个double类型的栈对象
    Stack<double> doubleStack(5);

    doubleStack.push(1.1);
    doubleStack.push(2.2);
    doubleStack.push(3.3);
    doubleStack.push(4.4);
    doubleStack.push(5.5);

    cout << "Popping values from doubleStack:\n";
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;

    return 0;
}

template<typename T>
class Stack {
    private:
        T *stackArray;
        int stackSize;
        int top;

    public:
        Stack(int size) {
            stackSize = size;
            stackArray = new T[stackSize];
            top = -1;
        }

        ~Stack() {
            delete [] stackArray;
        }

        void push(T value) {
            if (top == stackSize - 1) {
                cout << "Stack is full!\n";
            } else {
                top++;
                stackArray[top] = value;
            }
        }

        T pop() {
            if (top == -1) {
                cout << "Stack is empty!\n";
                return -1;
            } else {
                T value = stackArray[top];
                top--;
                return value;
            }
        }
};

int main() {
    Stack<int> intStack(5);

    intStack.push(10);
    intStack.push(20);
    intStack.push(30);
    intStack.push(40);
    intStack.push(50);

    cout << "Popping values from intStack:\n";
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;
    cout << intStack.pop() << endl;

    Stack<double> doubleStack(5);

    doubleStack.push(1.1);
    doubleStack.push(2.2);
    doubleStack.push(3.3);
    doubleStack.push(4.4);
    doubleStack.push(5.5);

    cout << "Popping values from doubleStack:\n";
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;
    cout << doubleStack.pop() << endl;

    return 0;
}

通俗来说,类模板是一种通用的类定义,它可以使用任何数据类型作为其成员的类型。在使用类模板时,需要将其中的占位符类型T替换为实际的数据类型。这样可以让我们编写一份通用的代码来处理不同类型的数据,而不必为每种数据类型都编写一份不同的代码。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值