c++ - 在 C++ 中,如果我有一个 vector 类型的 vector ,如果我在 : vector_variable. push_back() 之后执行此操作会发生什么?

标签 c++ vector std

std::vector<std::vector<Point2d> > components;

其中 Point2d 是一个结构:

struct Point2d {
int x;
int y;
};

std::vector<Point2d> tmp;
components.push_back( tmp );// What does this line do?

我知道问题出在我对 C++ vector 的理解上。 据我所知:在上面的代码中,Components 是 vector 的 vector 。内部 vector 是 Point2d 类型。内部 vector 类型的外部 vector 组。

在上面的代码中..Components.push_back(tmp)是做什么用的?

最佳答案

components 的大小增加 1。components 的最后一个元素是 Point2d 的 vector 。此 vector 是从空的 tmp 复制的。在 push_back 操作期间, vector components 可能需要重新分配存储,这可能会复制其元素并使其迭代器无效。

关于c++ - 在 C++ 中,如果我有一个 vector 类型的 vector ,如果我在 : vector_variable. push_back() 之后执行此操作会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28545906/

相关文章:

c++ - std::forward 如何推断 `_Ty` 的类型?

c++ - std::find 不使用我定义的 == 运算符

c++ - 无法使用 end() 获取 map 的第二个字段

c++ - 如何在 OS X 上获得准确的堆栈基地址?

c++ - 删除二维数组使用内存?

c++ - 维护递归计数

c++ - 重载给我错误 : no match for ‘operator<’

c++ - 访问 vector 时出现运行时错误

c++ - 为什么放置\n会改变执行顺序

c++ - 如何在类中初始化Eigen的SparseMatrix