DEV Community

Discussion on: What is Vector in C++? Get started in 5 minutes

Collapse
 
sandordargo profile image
Sandor Dargo

Nice summary. A few comments:

You can initialize a vector from an array, but that's not what happens at "1. Using an array".

If you want to show how to initialize a vector from an array, look at this example:

static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
Enter fullscreen mode Exit fullscreen mode

What happens in the example in this article is using the {}-initialization introduced by C++11 and that doesn't involve any arrays. Instead what you have on the right side is std::initializer_list and you use constructor #10 of vector: https://en.cppreference.com/w/cpp/container/vector/vector.

Sadly many articles on the net make the very same mistake (I guess one inspires the others).

Before I forget, vector does have a maximum size that's why there is max_size() that you also referenced.

Collapse
 
erineducative profile image
Erin Schaffer

Thank you for this information, Sandor! I appreciate it.