STL: Assign vector to array
-
I need to convert a vector to an array. It's no problem really, only one thing bothers me. If i use an iterator to iterate through the vector, i have no counter for the array, unless i declare an extra variable for that. If i use a counter to go through the vector, i bypass the recommended STL-way for iterating - iterators. So, does anyone know any elegant way for that ? I know that it's not really a problem, i'm just trying to optimize my solution. wbr Mr.Brainley
-
I need to convert a vector to an array. It's no problem really, only one thing bothers me. If i use an iterator to iterate through the vector, i have no counter for the array, unless i declare an extra variable for that. If i use a counter to go through the vector, i bypass the recommended STL-way for iterating - iterators. So, does anyone know any elegant way for that ? I know that it's not really a problem, i'm just trying to optimize my solution. wbr Mr.Brainley
A
vector
's storage is contiguous, so you can do&my_vector[0]
to get an (array-like) pointer to the first element.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
A
vector
's storage is contiguous, so you can do&my_vector[0]
to get an (array-like) pointer to the first element.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
Michael Dunn wrote:
A vector's storage is contiguous, so you can do &my_vector[0] to get an (array-like) pointer to the first element.
very encapsulated :suss:
-
Michael Dunn wrote:
A vector's storage is contiguous, so you can do &my_vector[0] to get an (array-like) pointer to the first element.
very encapsulated :suss:
The C++ Standard Library requires that the elements of a vector are in contiguous memory. There's no problem with code like Mike suggested and encapsulation has not been violated as the contiguous storage requirement is documented and required behaviour for a
std::vector
.Steve