OpenGL requires a continuous array of elements. There are apparently obvious reasons, there is no efficient way to insert one element into an adjacent array. This is necessarily at least O (N).
However, you could add N elements less than O (N ^ 2), which the vector reaches for N random inserts.
For example, if you donβt actually add new vertices βat any indexβ but are always close to the previous one, you can add all the elements to std :: list (O (1) for the element, O (N)), then copy std :: list in std :: vector. In fact, it should not be the previous element, just the previous element, so if the order is based on a recursive traversal of a tree, then you can still do it.
If new vertices are added at an index defined by some linear order, add all the elements in std :: map or std :: multi_map (O (log N) to the element, common O (N log N)), then copy it to the vector.
Thus, the method of performing the lowest complexity depends on how the order is determined. Whether these solutions with lower complexity are actually faster than the vector depends on N. They have much higher overhead (O (N) distribution instead of O (log N) for the vector), so N can be quite large up to asymptotic behavior is triggered.
If you use any of the solutions described by us, then a simple / effective way to copy a list or map into a vector is as follows:
std::vector<glVertex3f> vec; vec.reserve(listormap.size()); vec.insert(vec.begin(), listormap.begin(), listormap.end());
Steve jessop
source share