does vector <int> myVec [5] creates 5 vector?

and

list<int> myList[10] creates 10 linked list?

the [] and () seems to be very different.
does vector <int> myVec [5] creates 5 vector?

Yes.

list<int> myList[10] creates 10 linked list?

Yes.

the [] and () seems to be very different.

Indeed.
Last edited on
myVec is an array of 5 elements of type std::vector<int>

myList is an array of 10 elements of type std::list<int>

If we have:

 
double myarray[6];


what is myarray? There's no syntax difference in this to the others. All are an array of a specified size of a specified type.

Thank you for the answers. I learned something new today.
I would like to add an important fact which beginners don't know.
std::array (and simple array [...] like you showed) is a container that encapsulates fixed size arrays.
std::vector is a sequence container that encapsulates dynamic size arrays.
Last edited on
there is a time and a place for C style arrays, but those should be semi-rare and it is a little unusual to mix C arrays with C++ containers as you are doing here (its fine, its just a little weird and you would want to comment why you did it in real code).

I still use them probably more than I should. If I do not need anything that the C++ containers offer, I prefer to use a C array for the job; and its just a deep ingrained habit from before vector existed and even after it existed, it had poor performance for a while until the implementations caught up to the concept. Ive gotten a lot better about it, but it still comes right out my fingertips before my brain registers a 'hey.. wait..'.
Last edited on
std::vector<int> myVec[5] creates a C-style array with 5 elements, each element is a std::vector.

In essence a 2D container is being instantiated. The container has 5 rows, fixed. Rows can't be added or deleted.

The number of columns is variable, the std::vector holds the columns for each row, and at container creation the number of columns is zero (0). The number of columns can be changed as desired later, and each row can have a differing number of columns.

Same idea with std::list<int> myList[10], a 2D container with a fixed number of rows (10) and a variable number of columns using a std::list.

Mixing C and C++ containers for a multi-dimensional container is a bit odd, though it is doable. Better would be to use either C or C++ containers instead of mixing and matching.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <array>
#include <vector>
#include <list>

int main()
{
   // 2D container, 5 rows, fixed;
   // 1 column, can't change the number.
   int myArr[5][1];

   // 2D container, 5 rows, fixed;
   // zero columns that can be changed.
   std::array<std::vector<int>, 5> myVec;

   // 2D container, 10 rows, fixed;
   // zero columns that can be changed.
   std::array<std::list<int>, 10> myList;
}

Choosing the 2nd dimension container, std::vector or std::list, could depend on how you want to modify and access the elements.

Using std::array instead of a C-style/regular array makes using C++ algorithms easier and more generic.

std::array doesn't devolve to a pointer when passed into a function, unlike a regular array.


Topic archived. No new replies allowed.