I just got introduced to vector, what I understood is it is a structure like template that holds different data types, it's a dynamic array. My confusion is how is it different from template both are structures only.
A vector is both a template and a struct. However, a template is not necessarily a struct. This is not to be confused with a vector of structs.
Got that?
The struct that a vector uses is an "anchor" that holds information about the vector. What's contained in the anchor is implementation specific, but generally contains:
- A pointer to the dynamic memory allocated for the vector where the actual entries are stored
- A pointer to an allocator function if one is used
- The number of entries in the vector
- The capacity of the vector
- The size of entries in the vector.
You've probably learned about arrays. In C++, arrays have a fixed size.
You could write a class that implements a dynamic array of int. Vector is a template class that lets you implement a dynamic array of objects of any type.
So What I understand is vector is a template , ( but vice versa is not possible) , which is a dyanamic array that can hold any data type. And can we do actions like adding two numbers or some other work in this vector ?