c++ - vector <int> V[]和 vector <vector<int>> V的区别

标签 c++ arrays multidimensional-array vector

vector <int> V[]vector< vector<int> > V两者都是二维数组

但是它们之间的区别是什么?我们在哪里使用它们呢?请简要说明。

最佳答案

vector<int> V[]是一个 vector 数组

vector< vector<int> > V是一个 vector 的 vector

使用数组是C风格的编码,使用 vector 是C++风格的编码

引用 cplusplus.com ,

Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

TL;DR:

当您想使用固定数字 std::vector 时元素,您可以使用 vector <int> V[] .

当您想要使用std::vector动态数组 时, 你可以使用 vector< vector<int> > V .

关于c++ - vector <int> V[]和 vector <vector<int>> V的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28712364/

相关文章:

c++ - 我什么时候可以安全地从 C++ 中的基类继承

python - "AttributeError: ' 模块 ' has no attribute ' 播放器 '"尝试使用 Boost::Python 将 python 对象加载到 C++ 中时

c++ - 写入共享内存

php - 从数组 php 中的给定日期获取天数

c - 一维访问多维数组 : is it well-defined behaviour?

javascript - 重新调整 JavaScript 数组的尺寸(即 1x6 => 2x3)

c++ - 嵌套结构 : with invalid use of non-static member when define struct pointer

ruby - 如何在 Ruby 中正确访问和迭代多维数组?

javascript - 如何将数据从一个数组/对象推送到另一个数组/对象中?

c# - 试图 int.parse 多数组字符串