【发布时间】:2012-11-19 02:23:31
【问题描述】:
我创建了两个unique_ptr的标准向量:
std::vector<std::unique_ptr<Student>> students;
std::vector<std::unique_ptr<Teacher>> teachers;
然后,我创建一个新对象并将其放入向量中:
students.push_back(std::unique_ptr<Student> (new Student()));
teachers.push_back(std::unique_ptr<Teacher> (new Teacher()));
完成所有操作后,如何删除向量?
除了 unique_ptr 我必须做一个循环并删除每个对象:
while (!students.empty())
{
delete students.back();
students.pop_back();
}
现在有了 unique_ptr 我该怎么办?
我知道我必须使用 unique_ptr::reset(我认为)。
【问题讨论】:
标签: c++ vector c++11 std unique-ptr