Non-Growing Container

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Susman

    Non-Growing Container

    Hi,

    Say I have a vector v. Is there a way to limit its size (capacity?)
    initially so that it does not grow due to insertions? Would setting
    the capacity as a fixed number do the trick? If so, what happens when
    capacity is reached?

    Thanks.
  • Victor Bazarov

    #2
    Re: Non-Growing Container

    D. Susman wrote:
    Say I have a vector v. Is there a way to limit its size (capacity?)
    initially so that it does not grow due to insertions? Would setting
    the capacity as a fixed number do the trick? If so, what happens when
    capacity is reached?
    No, the standard containers do not have the ability to be limited.
    Use a regular array for that, or roll your own container wrapping
    std::vector, for example. Keep in mind that you shouldn't derive
    from it publicly because when you put a limitation on a type, it
    breaks the LSP. Derive privately and reimplement all functionality
    you need along with introducing the new features. You can easily
    implement the initial size and prohibit growth (by simply not
    implementing 'push_back' or 'insert').

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Eric.Malenfant@gmail.com

      #3
      Re: Non-Growing Container

      On Mar 12, 10:30 am, "D. Susman" <derya.sus...@g mail.comwrote:
      Hi,
      >
      Say I have a vector v. Is there a way to limit its size (capacity?)
      initially so that it does not grow due to insertions? Would setting
      the capacity as a fixed number do the trick? If so, what happens when
      capacity is reached?
      >
      As far as I know, not with std::vector. TR1 includes an "array"
      container. You may also be interested in Boost.Array (http://
      www.boost.org/doc/html/array.html) which, IIUC, served as a base for
      the TR1 array proposal.



      Comment

      • Bo Persson

        #4
        Re: Non-Growing Container

        D. Susman wrote:
        Hi,
        >
        Say I have a vector v. Is there a way to limit its size (capacity?)
        initially so that it does not grow due to insertions? Would setting
        the capacity as a fixed number do the trick? If so, what happens
        when capacity is reached?
        >
        You can use the reserve() fucntion to set a capacity.

        It is then up to you to decide what happens when you run out of
        capacity

        if (v.size() < v.capacity())
        {
        v.push_back(som ething);
        }
        else
        {
        // You decide what happens!
        }


        Bo Persson


        Comment

        • James Kanze

          #5
          Re: Non-Growing Container

          On Mar 12, 3:30 pm, "D. Susman" <derya.sus...@g mail.comwrote:
          Say I have a vector v. Is there a way to limit its size
          (capacity?) initially so that it does not grow due to
          insertions?
          Not really. You can probably come up with some way using a user
          defined allocator, however. But...
          Would setting the capacity as a fixed number do the trick? If
          so, what happens when capacity is reached?
          That's the real question. If you support insertion, do you want
          to limit the capacity, and if so, what do you do when you reach
          the limit? (It's easy to limit the capacity artificially, of
          course, by checking the size() before inserting. In which case,
          you can do whatever you want.)

          What problem are you trying to solve?

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...