当列表/数组中的元素数量可变时,哪种结构最适合实现?

Ath*_*vaI 2 c c++ arrays linked-list

I have a slightly specific question. I am using C/C++ with OpenCV. My aim to store detected rectangles in a list/array style structure. However, the length is variable for every frame (iteration).

What should I use to store this info? I thought of Linked Lists, however, they are slow to traverse and also if the number of nodes decrease, I will have to manually delete the extra nodes which would take up even more processor time. I discarded Arrays as they are not very flexible in terms of their length. I can do dynamic arrays with malloc but even with that I think I will need to specify the maximum number of elements.

feel free to correct me if i'm wrong somewhere. Also please do share your views and let me know what you think is the best way to go about this?

Thanks

编辑:我不限于C(我知道我提到了malloc).我可以使用C++功能以及我的程序的其余部分不使用任何C特定的功能.所以请随时向我推荐任何更好的方法.

Dan*_*röm 9

我建议你使用std::vector.a std::vector中的元素由C++标准保证是连续的,就像C 数组一样.这允许std::vector成为C++数据结构和C函数之间的接口.您可以使用它std::vector<T>::resize来确保在将其传递给OpenCV函数之前分配了空间.

哦,要获得指向矢量内部存储的指针,通常使用这种表示法:&rectangleCollection[0].

祝好运!