Like Tree1Likes
  • 1 Post By laserlight

purpose of capacity()having resize()

This is a discussion on purpose of capacity()having resize() within the C++ Programming forums, part of the General Programming Boards category; Hy all;;; Being novice to using vectors in Cpp ...I would like to know in more depth the purpose of ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    158

    Post purpose of capacity()having resize()

    Hy all;;;
    Being novice to using vectors in Cpp ...I would like to know in more depth the purpose of capacity() albeit we have resize() available...i mean that if i can resize the size i initially assigned to a vector than why do i need capacity or why this function is useful and when?(studied that capacity gives on hood memory which limits the size() function usage within available capacity)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    You might use the capacity() member function to determine if pushing back the next few elements would cause a re-allocation, hence invalidating say, iterators to your elements at a most inconvenient moment. If so, you could then reserve() the desired capacity, and then get your iterators, and then push back the elements. You might prefer this to actually resizing since the new size would cause new elements to be created immediately, when instead you want to push back or otherwise insert the elements.
    King Mir likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by laserlight View Post
    You might use the capacity() member function to determine if pushing back the next few elements would cause a re-allocation, hence invalidating say, iterators to your elements at a most inconvenient moment. If so, you could then reserve() the desired capacity, and then get your iterators, and then push back the elements. You might prefer this to actually resizing since the new size would cause new elements to be created immediately, when instead you want to push back or otherwise insert the elements.

    STILL not clear..A bit confusing..Sorry to say

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Each time you are resizing the vector (by calling resize() or push_back()) and new size is bigger then capacity you are facing the memory reallocation and probably memory moving.

    So if you are going to use this operations frequently - you will want to reserve enough capacity once and avoid memory operations on each resize.

    To know if you need to reserve more data or not - you will use capacity() member instead of size() member
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Well for one it's a good way to check how much space you have left before the next resize will be needed upon a push_back.

    One might, for example, use a vector to store particles from effects in a 3D game, and upon size reaching capacity, avoid creating more particles and instead note down that the vector will need growing after the current frame is rendered.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by iMalc View Post
    Well for one it's a good way to check how much space you have left before the next resize will be needed upon a push_back.

    One might, for example, use a vector to store particles from effects in a 3D game, and upon size reaching capacity, avoid creating more particles and instead note down that the vector will need growing after the current frame is rendered.


    That was what i needed to KNOW...THANKS

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Anyhow THANKS to all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disk capacity windows
    By ashaikh432 in forum C Programming
    Replies: 2
    Last Post: 08-03-2010, 12:54 AM
  2. what drive capacity would suit you?
    By happyclown in forum General Discussions
    Replies: 108
    Last Post: 08-26-2009, 10:23 AM
  3. capacity and reserve
    By George2 in forum C++ Programming
    Replies: 33
    Last Post: 03-06-2008, 07:14 PM
  4. Addition in less capacity computers
    By kkn in forum C Programming
    Replies: 8
    Last Post: 04-16-2006, 08:02 AM
  5. Retrieve total drive capacity
    By Markallen85 in forum Windows Programming
    Replies: 3
    Last Post: 08-18-2003, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21