1

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.

Community
  • 1
  • 1
codeLover
  • 3,720
  • 10
  • 65
  • 121

3 Answers3

2

Actually vectorObject[0] is a CustomClass *, so &vectorObject[0] is a CustomClass **.

You're making an interesting assumption here, that vector<> stores its elements sequentially. You're probably right though, so your code should work.

Edit: As per Ben Voigt's comments, the contiguousness of vector<> is guaranteed by the standard, so this method will 100% work.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • +1 for first observation. The last sentence is wrong though, there's no "probably" about it. `std::vector` is *required* to use sequential storage, to enable interop with old APIs that accept pointers. – Ben Voigt Nov 01 '12 at 17:37
  • @BenVoigt, not saying you're wrong, and intuitively it would make sense, but I don't see a requirement for it at http://www.sgi.com/tech/stl/Vector.html. The closest thing I can see is the sequence requirement of elements being arranged in strict linear order, but that's not strong enough. – Blindy Nov 01 '12 at 17:41
  • @BenVoigt, In fact on http://www.sgi.com/tech/stl/complexity.html it explicitly states you can implement `vector<>` using a `list<>`, performance implications aside. – Blindy Nov 01 '12 at 17:44
  • 1
    Read the linked question. Also note I wrote about `std::vector`, which is similar but not identical to the vector collection template in SGI STL. SGI documentation is not the definitive reference, the C++ Standard is. – Ben Voigt Nov 01 '12 at 18:04
  • I read that also, it was a proposal for C++11, not part of the current standard as far as I can tell. – Blindy Nov 01 '12 at 18:12
  • "The elements of a `vector` are stored contiguously, meaning that if `v` is a `vector` where `T` is some type other than `bool`, then it obeys the identity `&v[n] == &v[0] + n` for all `0 <= n < v.size()`." Direct quote from 23.3.6.1p1. You are aware that C++11 is the current Standard, right? But [the requirement is not new in C++11.](http://stackoverflow.com/questions/247738/is-it-safe-to-assume-that-stl-vector-storage-is-always-contiguous) – Ben Voigt Nov 01 '12 at 18:14
2

Yes, vectors are required to be consequtive in memory, so the expression "&vectorObject[0]" would return the address of the first element, in which you can point to.

dchhetri
  • 6,926
  • 4
  • 43
  • 56
2

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*>vectorObject Means that you have a vector of CustomClass Pointers to get an array

CustomClass **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 array

CustomClass * FirstEle = Array[0];

rerun
  • 25,014
  • 6
  • 48
  • 78