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?

Related Posts

  • 18 March

    Write Vector to File in C++

    Table of ContentsWrite Vector to File in C++ using vector indicesWrite Vector to File in C++ Using IteratorWrite Vector to File in C++ Using Range-Based for LoopWrite Vector to File in C++ Using the for_each() FunctionWrite Vector to File in C++ Without Using a LoopConclusionWas this post helpful? This article discusses different methods to Write […]

  • Remove element by value from vector in C++
    07 February

    Remove element by value in vector in C++

    Table of ContentsWhat are vectors in C++?Where can we remove an element from a vector?How to remove element by value in vector in C++?Using the std::find() function to remove element by value in vector in C++.Using the remove() function to remove element by value in vector in C++.Using the remove_copy() function to remove element by […]

  • Merge two vectors in C++
    11 June

    Merge two Vectors in C++

    Table of ContentsUsing merge() to merge two vectors in C++Using set_union() to merge two vectors in C++Using insert() to combine two vectors in C++Using move() & back_inserter to merge two vectors in C++Using copy() and back_inserter to concatenate two vectors in C++Using make_move_iterator() and insert() to merge two vectors in C++ConclusionWas this post helpful? vectors […]

  • Vector of Structs in C++
    18 May

    Vector of structs in C++

    Table of ContentsUsing Range ConstructorUsing Initializer listUsing custom constructorBonus ProgramConclusionWas this post helpful? In this article, we will see how to initialize and use vector of structs in C++. There are different ways to do it. This article will explain these methods clearly. Using Range Constructor We can utilize the range constructor for the initialization […]

  • Remove elements from vector in C++
    08 May

    How to remove element from a vector in C++

    How to remove elements from vector in C++

  • vector of vectors C++
    13 April

    How to create vector of vectors in C++

    Learn about how to create vector of vectors in C++.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.