PDA

View Full Version : Vector allocation



ToddAtWSU
22nd June 2007, 15:08
I have a vector I store a bunch of data points into. The problem I have is I have complex data and the easiest way for me to calculate the data ends up calculating the middle point first, then working to the end, then start back in the middle-1 spot to the beginning. I do this in 2 separate for-loops. I was storing this data into an array I allocated so I could access the middle of the array before adding all the data to the beginning first. After populating the entire array I would add a 3rd for-loop to push items 0-size into my vector. I would like to eliminate this array and store the data into my vector right away. I used the function vector->reserve( size ) to reserve the proper size. I check this with the capacity( ) function which says I have the number of elements I asked for. The problem I am having though is I cannot access the middle of the vector. I get some exception errors. So what I am asking is there a way I can add data to the middle of the vector before adding real data to the beginning since I have reserved the space for the vector? Thanks for your help!

high_flyer
22nd June 2007, 15:58
So what I am asking is there a way I can add data to the middle of the vector before adding real data to the beginning since I have reserved the space for the vector?
You mean so that the vector will increse in size?
Not that I know of.
Sound more as job for a linked list, not a vector.

TIP:
You will notice, that in the main key part of your keybard, on the right side there is a large button, with a "broken" arrow drawn on it - its called the "return" key.
It is there so that you can actually step to a start of a new line, not make your posts so hard to read, so please USE IT!

marcel
22nd June 2007, 18:02
QVector is a dynamic generic container, therefore its size will grow to fit the contents.
When you use QVector::reserve(N) it does not mean that you actually have N elements in the vector.
It just means that the object just adjusted its backstore to hold N items( without any items actually being there ). So you must populate the vector after calling reserve(). You can do this by hand or you can use QVector::fill(const &T val, int size). This initializes the first "size" elements of the vector to val, BUT it also resizes the vector to size. So you can use this instead reserve.

Then, you can use QVector::insert(int index, const &T).


Regards

Michiel
22nd June 2007, 18:48
I don't think he's talking about QVector. Just vector.

And I also think a linked list would be better suited for the job.

wysota
22nd June 2007, 21:37
I don't think he's talking about QVector. Just vector.
reserve() works in a simmilar way for all std::vector-like implementations (including QVector).


And I also think a linked list would be better suited for the job.

It depends. The drawback of using a linked list is that accessing items in a non-sequential order is slower. If you know the size of the data array you need upfront, using vector or an arraylist (like QList) should be better.