I understand “How to convert vector to array in C++“
answers how to convert a vector of doubles (NON POINTER TYPE) to an array.
My requirement :: To convert (a vector of CustomClass pointers) to (a CustomClass pointer to an array of CustomClass pointers).
Does the following code mean “(vector of pointers) –> (Pointer to an array of CustomClass pointers)”
std::vector <CustomClass*> vectorObject(SizeOfVector); // Here each element of the vector //is a pointer to CustomClass object.
CustomClass* customClassArray = &vectorObject[0];
Please correct me if I am wrong. Kindly Help with a code snippet.
Thanks in advance.
Vector is a wrapper in a sorts for an array and provides all the features of an array whilst also allowing the array to grow shrink know how big it is and a lot of other useful features. One of the requirements of std vector is that its data is stored in a contiguous fashion like an array. Because of this requirement you can get an array of elements by taking the address of the first element regardless of type.
std::vector <CustomClass*>vectorObjectMeans that you have a vector of CustomClass Pointers to get an arrayCustomClass **Array = &vectorObject[0]Now I have taken the contiguous data segment at offset 0 in the vector and assigned it the a pointer pointer of customclass remember that arrays and pointers are deeply connected in c and c++ I can now access the pointer pointer as if it were an arrayCustomClass * FirstEle = Array[0];