pop_back() vector class

This is a discussion on pop_back() vector class within the C++ Programming forums, part of the General Programming Boards category; I am supposed to implement the void pop_back (); function from the vector class for a project. Does anybody know ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    27

    pop_back() vector class

    I am supposed to implement the void pop_back (); function from the vector class for a project. Does anybody know how to implement this function that removes the last element from the Vect.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    You implemented your own vector class? If so, how did you implement it?
    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
    Jan 2005
    Posts
    7,317
    The pop_back() function simply calls the destructor for the last element in the vector and updates the vector's internal state to have a size that is one smaller than it was before. How you do that in code depends on how vector is implemented. Even if you didn't write your own vector, each implementation can implement it differently, so there isn't really a single answer.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    27
    So say my private variables are
    num // # of elements in the array
    cap // size of the vector
    T * data // the dynamic array

    should I do something like

    Code:
      T * temp = new T[cap];
      for (int i = 0; i < num - 1; ++i)
      {
        temp[i] = data[i];
      }
      delete[] data;
      data = temp;
    Or am I completely not understanding what you are telling me?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    you could it that way but a more efficient method would be to call the destructor on the last element and then decrement num (making sure that the vector is not empty beforehand, of course).
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    27
    I'm having a hard time understanding what you are saying and putting that into code.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    If num > 0, decrement num (i.e. --num) then call the destructor for data[num] (e.g. data[num].~T() ). If not, then either do nothing or indicate some error.
    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

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I'm having a hard time understanding what you are saying and putting that into code.
    Explicitly calling the destructor for an object is probably the way real implementations handle this, but it is a very advanced solution, so if your programming class isn't that advanced, then your original idea might be what the instructor is looking for. If you do use your original idea, remember that you must decrement cap.

Popular pages Recent additions subscribe to a feed

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