Results 1 to 5 of 5

Thread: Vector allocation

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Unhappy Vector allocation

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Vector allocation

    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!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Vector allocation

    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

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Vector allocation

    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.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Vector allocation

    Quote Originally Posted by Michiel View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.