Pointer template

Original link: http://www.cnblogs.com/binterminator/articles/1598240.html
ExpandedBlockStart.gif ExpandedBlockStart.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); // points to where to modify, not pointing to the content . 17 18 is const char * in [the 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
 
Use c ++ template pointer, deep attention to the heap memory copy! !

Reproduced in: https: //www.cnblogs.com/binterminator/articles/1598240.html

Guess you like

Origin blog.csdn.net/weixin_30892889/article/details/94805627