Thread: purpose of capacity()having resize()

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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, 08: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