In C + +, we cannot initialize another array directly with an array, we can only create new arrays, and then explicitly copy the elements of the original array to the new array individually.
In accordance with the practice in the C language:
Const size_t arry_size=6;
int int_arry[arry_size]={0,1,2,3,4,5};
int int_arry2[arry_size];
for (size_t ix=0;ix<arry_size;++i)
Int_arry2[ix]=int_arry[i];
Instead of initializing the vector object with an array, you can initialize another vector object directly using one of the vector objects:
#include <iostream>
#include <vector>
using namespace Std;
int main () {
Initializing a Vector object with an array
const size_t arr_size=6;
int int_arr[arr_size]={0,1,2,3,4,5};
vector<int> Ivec (int_arr,int_arr+arr_size);
/* The two pointers passed to Ivec mark the range of the vector's initial value. The second pointer points to the address space after the last element being copied. */
The range of elements being marked can be a subset of the array
vector<int> ivec1 (IVEC);
For (Vector<int>::size_type i=0;i!=ivec1.size (); ++i)
cout<<ivec1[i];
cout<<endl;
return 0;
}
After the array is converted to a vector object, you can use various function operations of the vector object, such as size () to get the number of elements, push_back () does not add new elements at the end, and so on.
Using vector containers instead of arrays-initializing vector objects with arrays