So my friend and I were looking at the std::vector
class the other day and noticed that you can use the function std::vector::data
to retrieve a pointer to the internal array in c++11. Intuitively this seems somewhat redundant as you can access/update the contents of the internal array using the []
on the vector anyway.
It just seems like bad practice to me to "stick your hand into" an STL container.
Anyway, there must be some good applications of it. So my question is, what are those applications?
Thanks!
Reference: http://www.cplusplus.com/reference/vector/vector/data/
For example if you need to pass the data to a function that only takes a pointer. OpenGL for example has a bunch of them (glBufferData
for example). std::vector
makes the memory management easy, so it's a good choice instead for a container instead of managing the memory oneself and potentially leaking it somewhere. It's not meant that you mutate the vectors content through the returned pointer, but rathe read it and/or pass it to functions expecting a pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With