- #1
ehrenfest
- 2,020
- 1
Code:
std::vector<float> *my_vectors;
does this create a vector of pointers or a pointer to a vector?
std::vector<float> *my_vectors;
No. The statement int index; creates a variable named index. While this variable is associated with a chunk of memory, this declaration statement does not allocate memory, at least not in the sense you are thinking. I certainly hope you will not write code that accesses the value of the variable index before you assign it a value.ehrenfest said:OK. So it actually allocates the memory for the pointer and also allocates the memory for the vector that is pointed to, right?
D H said:his declaration statement associates a variable with a chunk of memory. It does not, however, allocate memory, at least not in the sense you appear to be thinking.
The purpose of this code is to create a pointer to a vector that contains floating-point numbers. The vector can then be used to store and manipulate a collection of float values.
A vector of floats is a dynamic data structure, meaning its size can change at runtime, whereas a regular array has a fixed size. Vectors also have built-in functions for adding, removing, and accessing elements, making them more versatile than arrays.
Pointers allow for more efficient use of memory and can be passed as function parameters, whereas a regular vector is copied each time it is passed. Pointers also allow for easier swapping or reassigning of vectors.
To add elements to a vector, you can use the push_back()
function, which adds the element to the end of the vector. Alternatively, you can use the insert()
function to add an element at a specific position in the vector.
No, a vector of floats can only store floating-point numbers. If you need to store integers or other data types, you would need to create a separate vector for each data type or use a different data structure such as a map or list.