size vs. capacity in a container, such as vector

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

    #1

    size vs. capacity in a container, such as vector

    Say I have a vector<intintVe c, and I reserve some sapce

    intVec.reserve( 100);

    Is it fair to assume the vector contains enough space for 100
    elements. In which case, the capacity -- ability to store 100 elements
    without a relocation -- is 100.

    Then what is the difference for intVec.resize(1 00)?

    Just trying to understand the difference between these two beasts,
    size and capacity.


    Thank you.
  • Ian Collins

    #2
    Re: size vs. capacity in a container, such as vector

    puzzlecracker wrote:
    Say I have a vector<intintVe c, and I reserve some sapce
    >
    intVec.reserve( 100);
    >
    Is it fair to assume the vector contains enough space for 100
    elements. In which case, the capacity -- ability to store 100 elements
    without a relocation -- is 100.
    >
    Then what is the difference for intVec.resize(1 00)?
    >
    Just trying to understand the difference between these two beasts,
    size and capacity.
    >
    reserve() reserves space (capacity), but size() will not change.

    try:

    #include <vector>
    #include <iostream>

    int main()
    {
    std::vector<int v1;
    std::vector<int v2;

    v1.reserve(100) ;
    v2.resize(100);

    std::cout << "v1 " << v1.size() << ' ' << v1.capacity() << std::endl;
    std::cout << "v2 " << v2.size() << ' ' << v2.capacity() << std::endl;
    }

    --
    Ian Collins

    Comment

    • Victor Bazarov

      #3
      Re: size vs. capacity in a container, such as vector

      puzzlecracker wrote:
      Say I have a vector<intintVe c, and I reserve some sapce
      >
      intVec.reserve( 100);
      >
      Is it fair to assume the vector contains enough space for 100
      elements. In which case, the capacity -- ability to store 100 elements
      without a relocation -- is 100.
      >
      Then what is the difference for intVec.resize(1 00)?
      >
      Just trying to understand the difference between these two beasts,
      size and capacity.
      Try to be curious enough to experiment...

      std::vector<int intVec;
      std::cout << intVec.size() << std::endl;
      intVec.reserve( 100);
      std::cout << intVec.size() << std::endl;
      intVec.resize(1 00);
      std::cout << intVec.size() << std::endl;

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


      Comment

      • Juha Nieminen

        #4
        Re: size vs. capacity in a container, such as vector

        puzzlecracker wrote:
        Say I have a vector<intintVe c, and I reserve some sapce
        >
        intVec.reserve( 100);
        >
        Is it fair to assume the vector contains enough space for 100
        elements. In which case, the capacity -- ability to store 100 elements
        without a relocation -- is 100.
        >
        Then what is the difference for intVec.resize(1 00)?
        The difference is that, among other things, the new elements will
        *not* be initialized with reserve(), and the push_back() and at()
        functions will behave differently. Accessing elements beyond size() with
        operator[] (even if space has been reserved for those elements) is
        probably undefined behavior, especially if the element type is a class
        which requires initialization.

        Comment

        • James Kanze

          #5
          Re: size vs. capacity in a container, such as vector

          On Oct 12, 10:08 am, Juha Nieminen <nos...@thanks. invalidwrote:
          puzzlecracker wrote:
          Say  I have a vector<intintVe c, and I reserve some sapce
          intVec.reserve( 100);
          Is it fair to assume the vector contains enough space for
          100 elements. In which case, the capacity -- ability to
          store 100 elements without a relocation -- is 100.
          Then what is the difference for  intVec.resize(1 00)?
          The difference is that, among other things, the new elements
          will *not* be initialized with reserve(), and the push_back()
          and at() functions will behave differently. Accessing elements
          beyond size() with operator[] (even if space has been reserved
          for those elements) is probably undefined behavior, especially
          if the element type is a class which requires initialization.
          There's no probably...espe cially about it. It's undefined
          behavior (except for at(), which is guaranteed to throw). In
          any good implementation, something like

          int
          main()
          {
          std::vector< int v ;
          v.reserve( 100 ) ;
          v[ 50 ] ;
          return 0 ;
          }

          will result in a runtime error (core dump under Unix).

          More generally, there are two main uses of reserve(): to ensure
          the validity of iterators and pointers to elements, and as an
          optimization measure. It doesn't affect the logical state of
          the container.

          --
          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

          • Hendrik Schober

            #6
            Re: size vs. capacity in a container, such as vector

            puzzlecracker wrote:
            [...]
            Just trying to understand the difference between these two beasts,
            size and capacity.
            One is the number of objects the vector actually has, the
            other is the number of objects the vector could have without
            needing to allocate more memory.
            Thank you.
            HTH,

            Schobi

            Comment

            Working...