为什么这个c ++程序给出了seg错误

Dev*_*wal 0 c++

为什么这个程序给出分段错误.我正在为20个字符串分配内存.(默认情况下也是20).并设置并尝试访问第20个元素.

#include <iostream>
using namespace std;


class myarray
{
  private:
    string *items;
  public:

    myarray (int size=20)
    {
      items = new string[size];
    }

    ~myarray()
    {
      delete items;
    }

    string& operator[] (const int index)
    {
      return items[index];
    }
    /* 
    void setvalue (int index, string value)
    {
      items[index] = value;
    }

    string getvalue (int index)
    { 
      return items[index];
    }
    */

};


int main()
{
  myarray m1(20);
  myarray m2;
  m1[19] = "test ion";
  cout << m1[19];
  //m1.setvalue (2, "Devesh ");
  //m1.setvalue (8, "Vivek ");
  //cout << m1.getvalue(19);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

teh*_*exx 7

如果你分配一个像你正在做的数组,new string[size]你需要使用delete[] items;