指针模板

本文详细介绍了C++中使用模板实现栈的数据结构,并深入探讨了深拷贝在模板类中的应用,通过具体代码示例展示了如何正确处理堆上内存的复制,避免浅拷贝带来的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExpandedBlockStart.gifExpandedBlockStart.gif
 1 //main.cpp
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <ctime>
 5 #include "stcktp1.h"
 6 
 7 
 8 const int Num = 10;
 9 int main()
10 {
11     std::srand(std::time(0));
12     std::cout<<"Please enter stack size.\n";
13     int stacksize;
14     std::cin>>stacksize;
15     
16     Stack<const char *> st(stacksize);//指向何处可修改,指向内容不可以
17     
18     const char * in[Num] = {
19         "1:abc","2:5848","3","4","5","6","7","8","9","10"
20     };
21     const char * out[Num];
22     int processed = 0;
23     int nextin = 0;
24     while(processed<Num)
25     {
26         if(st.isempty())
27             st.push(in[nextin++]);
28         else if(st.isfull())
29             st.pop(out[processed++]);
30         else if((std::rand())%2 && nextin < Num)
31             st.push(in[nextin++]);              //50-50的概率执行
32         else 
33             st.pop(out[processed++]);
34     }
35     for(int i=0;i<Num;i++)
36         std::cout<<out[i]<<std::endl;
37         
38         std::cout<<"BYE\n";
39         std::cin.get();
40         std::cin.get();
41         std::cin.get();
42         
43         return 0;
44     
45     
46 }

 1 //stcktp1.h
 2 #ifndef STCKTP1_H_
 3 #define STCKTP1_H_
 4 
 5 template <class T>
 6 class Stack
 7 {
 8     private:
 9         enum {SIZE = 10};
10         int stacksize;
11         T * items;
12         int top;
13     public:
14         explicit Stack(int ss=SIZE);
15         Stack(const Stack &st);
16         ~Stack(){delete[] items;}
17         bool isempty() {return top==0;}
18         bool isfull() {return top==stacksize;}
19         bool push(const T & item);
20         bool pop(T & item); 
21         Stack & operator =(const Stack & st);  //保护参数不被修改        
22 };
23 
24 template <class T>
25 Stack<T>::Stack(const Stack &st)
26 {
27     stacksize = st.stacksize;
28     top=st.top;
29     items = new T[stacksize]; //深度copy 
30     for(int i=0;i<top;i++)
31         items[i] = st.items[i];  
32 }
33 
34 template <class T>
35 Stack<T>::Stack(int ss):stacksize(ss),top(0)//初始化方式 
36 {
37     items = new T[stacksize];
38 }
39 
40 
41 template <class T>
42 bool Stack<T>::push(const T & item)
43 {
44     if(top<stacksize)
45     {
46         items[top++= item;
47         return true;
48     }
49     else
50         return false;
51 }
52 
53 
54 template <class T>
55 bool Stack<T>::pop(T & item)
56 {
57     if(top>0)
58     {
59         item = items[--top];
60         return true;
61     }
62     else
63         return false;
64 }
65 
66 template <class T>
67 Stack<T> & Stack<T>::operator=(const Stack<T> &st)
68 {
69     if(this == &st)
70         return *this;
71     delete []items;
72     stacksize = st.stacksize;
73     top = st.top;
74     items = new T[stacksize];
75     for(int i=0;i<top;i++)
76         items[i]=st.items[i];
77     return *this;
78 }
79 
80 #endif
81 
c++模板指针的使用,注意堆上内存的深拷贝!!

转载于:https://www.cnblogs.com/binterminator/articles/1598240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值