我明白了“How to convert vector to array in C++”
回答了如何将 double vector (非指针类型)转换为数组。
My requirement :: To convert (a vector of CustomClass pointers) to (a CustomClass pointer to an array of CustomClass pointers).
下面的代码是否表示“(指针 vector )-->(指向 CustomClass 指针数组的指针)”
std::vector <CustomClass*> vectorObject(SizeOfVector); // Here each element of the vector //is a pointer to CustomClass object.
CustomClass* customClassArray = &vectorObject[0];
如有错误请指正。请帮助提供代码片段。
提前致谢。
最佳答案
Vector 是数组的一种包装器,它提供数组的所有功能,同时还允许数组增长缩小知道它有多大以及许多其他有用的功能。 std vector 的要求之一是它的数据像数组一样以连续的方式存储。由于此要求,您可以通过获取第一个元素的地址来获取元素数组,而不考虑类型。
std::vector <CustomClass*>vectorObject
意味着你有一个 CustomClass 指针的 vector 来获取一个数组
CustomClass **Array = &vectorObject[0]
现在我已经获取了 vector 中偏移量 0 处的连续数据段并将其分配给 customclass 的指针指针记住数组和指针在 c 和 c++ 中是深度连接的我现在可以像访问数组一样访问指针指针
CustomClass * FirstEle = Array[0];
关于c++ - 如何转换(指针 vector )-->(指向指针数组的指针),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182389/