Initializing a Vector with the Contents of an Array

Initializing a Vector with the Contents of an Array

You can initialize a vector with the contents of a built-in array like this:

 #include#include using namespace std;int main(){  int arr[3];  arr[0] = 4; arr[1] = 8;  arr[2] = 16;   vector  vi ( &arr[0],     // first argument is the address of  the array's beginning                             &arr[3] );    // second argument must point one element past the array's end!   cout<< vi[0] << '	' << vi[1] << '	' << vi[2] <
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist