Vector as a class member

后端 未结 4 1308
醉梦人生
醉梦人生 2020-12-15 13:36

Hello I have this question: I would like to have a vector as class member. This is perhaps my question easier for you and I apologize for that.

  • how should I de
4条回答
  •  一生所求
    2020-12-15 14:18

    Most of the time, when we use standard library, We do not need to care about the memory allocation/deallocation. The template will handle it automatically. eg. The memory of a std::vector will be increase or decrease according to the elements stored in this vector. This would be an example.

    Therefore, almost you can use it this way in your case.

    std::vector myVector  //your second declaration
    if(myCondition)
    {
       myVector.push(some_int);  // use it directly
    }
    

    The memory the vector used will be deallocated when the Class object you created is destroyed.

提交回复
热议问题