gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:23:25 33 4
gpt4 key购买 nike

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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com