Original Post
I have a question about C++ std::map.
When you use vector it's a growing array. It reallocates a block of memory for storage if needed. I can remove elements from the back of the list and the size of the vector grows smaller. The memory that is allocated to it stays constant though which is one nice thing about vectors. They are efficient for certain cases because of this. If I need to add more elements back in to the array the memory doesn't need to be reallocated.
Now let's say I have a map of some int to a vector.
std::map >
I put a mapping of 5 to a new std::vector because currently there is no vector in the map at 5 yet.
Now let's say I clear the map. Was the memory allocated for storing the vector freed or is that memory still around and will be reused for a new element in the map?
Let's say I now put a new vector with a key of 4. Will the memory that was allocated for the vector stored at 5 be reused for the vector stored at 4?
I hope this question isn't too confusingly worded.
The reason I'm asking is this is something used by my rendering engine and the std::map stores which static triangle indeces are in the view frustum this frame. It's a mapping of material index to lists of triangle indeces so I can sort the triangles I render by material easily to avoid switching active textures and shaders. This will be updated every frame and inefficiency in this area due to constant memory reallocation could severely hamper the FPS. I might try to use more efficient data structures in the future but for now my engine is in the early stages and I'm using the STL for most things. I am also open to suggestions of more efficient data structures to use in place of a map if anyone has good ideas.
When you use vector it's a growing array. It reallocates a block of memory for storage if needed. I can remove elements from the back of the list and the size of the vector grows smaller. The memory that is allocated to it stays constant though which is one nice thing about vectors. They are efficient for certain cases because of this. If I need to add more elements back in to the array the memory doesn't need to be reallocated.
Now let's say I have a map of some int to a vector.
std::map
I put a mapping of 5 to a new std::vector because currently there is no vector in the map at 5 yet.
Now let's say I clear the map. Was the memory allocated for storing the vector freed or is that memory still around and will be reused for a new element in the map?
Let's say I now put a new vector with a key of 4. Will the memory that was allocated for the vector stored at 5 be reused for the vector stored at 4?
I hope this question isn't too confusingly worded.
The reason I'm asking is this is something used by my rendering engine and the std::map stores which static triangle indeces are in the view frustum this frame. It's a mapping of material index to lists of triangle indeces so I can sort the triangles I render by material easily to avoid switching active textures and shaders. This will be updated every frame and inefficiency in this area due to constant memory reallocation could severely hamper the FPS. I might try to use more efficient data structures in the future but for now my engine is in the early stages and I'm using the STL for most things. I am also open to suggestions of more efficient data structures to use in place of a map if anyone has good ideas.