How to Convert Vector to Array in C++

  1. Use the data() Method to Convert a Vector to an Array
  2. Use & Address of Operator to Convert a Vector to an Array
  3. Use copy() Function to Convert a Vector to an Array

This article will introduce how to convert a vector to an array in C++.

Use the data() Method to Convert a Vector to an Array

Since the C++ Standard guarantees that vector container elements are stored contiguously in memory, we can call the data, a built-in vector method, and assign the returned address to the newly declared double pointer, as shown in the following code sample.

Note that any modifications to the elements using the d_arr pointer will alter the data elements of the original vector because we are merely pointing to the arr elements but not copying them to a new location.

#include <iostream>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

int main()
{
    vector<double> arr {-3.5, -21.1, -1.99, 0.129, 2.5, 3.111};

    copy(arr.begin(), arr.end(),
         std::ostream_iterator<double>(cout,"; "));
    cout << endl;

    double *d_arr = arr.data();
    for (size_t i = 0; i < arr.size(); ++i) {
        cout << d_arr[i] << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

-3.5; -21.1; -1.99; 0.129; 2.5; 3.111;
-3.5; -21.1; -1.99; 0.129; 2.5; 3.111;

Use & Address of Operator to Convert a Vector to an Array

Alternatively, we can use the ampersand operator, which takes the address of the object in the memory and assign it to a newly declared pointer to a double.

In this example, we are taking the address of the first element in vector, but you can extract a pointer to any other element and do operations as needed.

Elements can be accessed with d_arr[index] array notation, and similarly to the previous method, any modifications to d_arr are affecting arr data.

#include <iostream>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

int main()
{
    vector<double> arr {-3.5, -21.1, -1.99, 0.129, 2.5, 3.111};

    copy(arr.begin(), arr.end(),
         std::ostream_iterator<double>(cout,"; "));
    cout << endl;

    double *d_arr = &arr[0];
    for (size_t i = 0; i < arr.size(); ++i) {
        cout << d_arr[i] << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Use copy() Function to Convert a Vector to an Array

The copy() method can be utilized to convert a vector to a double array so that data elements are copied to a different memory location. Later, we can modify them without worrying about altering the original vector data.

Notice that we are declaring a d_arr with fixed 6 elements, which is allocated as stack memory. In most scenarios, the size of an array will not be known in advance, so you would need to use dynamic memory allocation with methods like new or malloc.

#include <iostream>
#include <vector>
#include <iterator>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

int main()
{
    vector<double> arr {-3.5, -21.1, -1.99, 0.129, 2.5, 3.111};

    copy(arr.begin(), arr.end(),
         std::ostream_iterator<double>(cout,"; "));
    cout << endl;

    double d_arr[6];
    copy(arr.begin(), arr.end(), d_arr);
    for (size_t i = 0; i < arr.size(); ++i) {
        cout << d_arr[i] << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

new_vec - | 97 | 98 | 99 | 100 | 101 |

Related Article - C++ Vector

  • How to Initialize a Vector in C++
  • comments powered by Disqus