Difference between vector V[] and vector< vector> > V

前端 未结 5 1702
陌清茗
陌清茗 2020-12-28 17:06

vector V[] and vector< vector > V both are 2D array.

But what is the differenc

5条回答
  •  孤城傲影
    2020-12-28 17:46

    vector V[] is an array of vectors.

    vector< vector > V is a vector of vectors.

    Using arrays are C-style coding, using vectors are C++-style coding.

    Quoting 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:

    When you want to work with a fixed number of std::vector elements, you can use vector V[].

    When you want to work with a dynamic array of std::vector, you can use vector< vector > V.

提交回复
热议问题