我刚开始使用 STL,假设我有一个兔子课程,现在我正在创建一个兔子军队......
#include <vector>
vector<rabbit> rabbitArmy (numOfRabbits,rabbit());
//Q1: these rabbits are on the heap right?
rabbit* rabbitOnHeap = new rabbit();
//Q2: rabbitOnHeap is on the heap right?
rabbit rabbitOnStack;
//Q3: this rabbit is on the stack right?
rabbitArmy.push_back(rabbitOnStack);
//Q4: rabbitOnStack will remain stored on the stack?
//And it will be deleted automatically, though it's put in the rabbitArmy now?
Q4是我最关心的一个,我应该总是使用new关键字来添加我的军队吗?
Q5:有没有比增加兔子更好的方法:
rabbitArmy.push_back(*rabbitOnHeap);