鱼C论坛

 找回密码
 立即注册
查看: 1793|回复: 3

请问C++里面的 std::string 可否当做类型来定义数组?

[复制链接]
发表于 2014-11-22 15:31:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x


  俺写了个小例子 在下面  如果把string当成个类型  比如 后面 注释掉的 Stack<string> B(9) ; 来构造一个堆栈类的数组 array  编译通过但是运行就崩溃了    这是什么原理   请前辈指教   


#include<iostream>

using namespace std ;

template <class T>
class Stack
{
  private:
       int size ;
       int top = 0 ;
       T *array ;
public :
       Stack(int size)
          {
            this->size = size ;
            array = new T[size] ;
          }
       ~Stack() {    delete []array ;    }

       void push(T data) {    array[top++] = data ;   }

       T pop()
          {
              top-- ;
              cout<<" we get "<<array[top]<<endl;  
          }            
};

int main()
{
   Stack<int> A(7) ;
   A.push(22) ;
   A.push(44) ;
   A.push(66) ;


   A.pop() ;
   A.pop() ;
   A.pop() ;  

// Stack<string> B(9) ;
// B.push("string_1") ;
// B.push("string_2") ;
// B.push("string_3") ;

// B.pop();
// B.pop();
// B.pop();

   Stack<char> C(10) ;
   C.push('a') ;
   C.push('b') ;  
   C.push('c') ;

   C.pop() ;
   C.pop() ;
   C.pop() ;
}

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-22 16:17:25 | 显示全部楼层
可以
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-11-22 17:39:24 | 显示全部楼层


  请教老大   string 可是大小不定的啊 这要在 ansi C 里面可就麻烦了吧     

  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-22 18:40:29 | 显示全部楼层
  1. #include<iostream>
  2. #include <string>
  3. using namespace std ;

  4. template <class T>
  5. class Stack
  6. {
  7.    private:
  8.            int size ;
  9.            //int top = 0 ;
  10.            int top;
  11.            T *array ;
  12.    public :
  13.            //Stack(int size)
  14.            Stack(int size):top(0)
  15.            {
  16.                    this->size = size ;
  17.                    array = new T[size] ;
  18.            }
  19.            ~Stack() {    delete []array ;    }
  20.           
  21.        void push(T data) {    array[top++] = data ;   }
  22.           
  23.        T pop()
  24.            {
  25.                    top-- ;
  26.                    cout<<" we get "<<array[top]<<endl;  
  27.                    return array[top];
  28.            }            
  29. };

  30. int main()
  31. {
  32.     Stack<int> A(7) ;
  33.     A.push(22) ;
  34.     A.push(44) ;
  35.     A.push(66) ;
  36.        
  37.        
  38.         A.pop() ;
  39.     A.pop() ;
  40.     A.pop() ;  
  41.        
  42.         Stack<string> B(9) ;
  43.         B.push("string_1") ;
  44.         B.push("string_2") ;
  45.         B.push("string_3") ;
  46.        
  47.         B.pop();
  48.         B.pop();
  49.         B.pop();
  50.        
  51.         Stack<char> C(10) ;
  52.     C.push('a') ;
  53.     C.push('b') ;  
  54.     C.push('c') ;
  55.        
  56.         C.pop() ;
  57.     C.pop() ;
  58.     C.pop() ;
  59. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 04:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表