Convert Vector to Array in C++

In this post, we will see how to convert vector to Array in C++.

Vectors and Arrays in C++

We can use an array to store a collection of data belonging to a primitive data type. It stores the data in a contiguous memory location. Vectors are very similar to arrays.

We can assume vectors to be dynamic arrays that can be resized according to the requirements. They are not present in C and are only available in C++. There are several methods associated with vectors in C++.

Convert Vector to Arrays in C++

In this article, we will discuss different methods to convert vector to array in C++.

Using the for loop to convert vector to array in C++

In this method, we will traverse through the vector and copy every element individually to a new array.

See the code below.

Output:

5
7
8
2

In the above example,

  • We defined a vector v that stores some integers.
  • Using the size() function we find the length of the vector.
  • We create an array a of the same size.
  • We use the for loop to iterate over the vector and assign every element to the array.
  • The array now contains all the elements of the vector and we display it.

Using the copy() function to convert vector to array in C++

In C++, the copy() function can copy the elements from one object to another based on some provided range. It is defined in the algorithm header file.

For example,

Output:

5
7
8
2

In the above example,

  • The copy() function will copy the elements from the vector v to the array a.
  • In the copy() function, the v.begin() function provides the iterator pointing to the initial element of vector v.
  • Similarly, the end() function returns the iterator pointing to the last element.

Using the transform() function to convert vector to array in C++

We can use the transform() function in C++ to apply a function to all the elements of an array. It applies the function to all the elements and stores the result in a new array.

We can use it to convert vector to array. We will apply a single-line function that returns the element of the vector and store it in the array.

Here also we will specify the range of elements using the begin() and end() function, as we did in the previous method.

See the code below.

Output:

5
7
8
2

Using the data() function to convert vector to array in C++

We can use the data() function to return the memory location of the vector in a double pointer. We can assign this to a pointer and represent the internal array of the vector. Any modifications made to this will be reflected in the original vector.

See the following example,

Output:

5
7
8
2

In the above code,

  • The data() function returns the memory address of vector v.
  • We use this to create an array instance.
  • The size_t in the for loop is used to specify the byte size of memory to iterate over using memory location and not the index.

Using the & operator to convert vector to array in C++

The & operator returns the address of an object in C++. As discussed in the previous method, we will return the address of the first element of the vector to a pointer to represent the internal array structure of a vector.

For example,

Output:

5
7
8
2

As discussed in the previous method, any changes made will reflect on the original vector.

Conclusion

In this article, we discussed how to convert vector to array in C++. First, we discussed arrays and vectors noting their similarities and differences. The initial methods involving the for loop, copy() function, and transform() function created a new array using the elements of the vector.

The final two methods achieved this using the memory location of the vector, so any changes made in the array were reflected in the original vector.

That’s all about how to convert Vector to Array in C++.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *