What is the C ++ apvector

1

The college professor asked to search the sorting scripts and run tests with them, determining which is best in what situation.

I found the scripts, however on the site the guy does not show how to run , and he uses a apvector I do not know where it comes from, so I can not execute the scripts.

Is this apvector any library? Can I run this script?

// Bubble Sort Function for Descending Order 
void BubbleSort(apvector <int> &num)
{
      int i, j, flag = 1;    // set flag to 1 to start first pass
      int temp;             // holding variable
      int numLength = num.length( ); 
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (num[j+1] > num[j])      // ascending order simply changes to <
              { 
                    temp = num[j];             // swap elements
                    num[j] = num[j+1];
                    num[j+1] = temp;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned
}
    
asked by anonymous 06.06.2016 / 18:26

1 answer

1

The Apvector class is based on the Vector class, and all methods belonging to it are also part of Vector. It is a template that you can specify with which type of collection it will work on.

Some features of Apvector:

  • They are like Arrays and their index starts from scratch
  • Can be initialized to maintain the same value in each index, providing a second argument to the constructor while the vector is defined.
  • Can be resized
  • Must be passed by reference
  • The following is the implementation model for the class Apvector

    // *******************************************************************
    //  APCS vector class  IMPLEMENTATION
    //
    //  see vector.h for complete documentation of functions
    //
    //  vector class consistent with a subset of the standard C++ vector class
    //  as defined in the draft ANSI standard (part of standard template library)
    // *******************************************************************
    
    
    #include <stdlib.h>
    #include <assert.h>
    #include <iostream.h>
    #include "apvector.h"
    
    template <class itemType>
    apvector<itemType>::apvector()
    //postcondition: vector has a capacity of 0 items, and therefore it will
    //               need to be resized
        : mySize(0),
          myList(0)      
    {
    
    }
    
    template <class itemType>
    apvector<itemType>::apvector(int size)
    // precondition: size >= 0
    // postcondition: vector has a capacity of size items
       : mySize(size),
         myList(new itemType[size])
    {
    
    }
    
    template <class itemType>
    apvector<itemType>::apvector(int size, const itemType & fillValue)
    // precondition: size >= 0
    // postcondition: vector has a capacity of size items, all of which are set
    //                by assignment to fillValue after default construction
        : mySize(size),
          myList(new itemType[size])
    {
        int k;
        for(k = 0; k < size; k++)
        {
            myList[k] = fillValue;
        }
    }
    
    template <class itemType>
    apvector<itemType>::apvector(const apvector<itemType> & vec)
    // postcondition: vector is a copy of vec
        : mySize(vec.length()),
          myList(new itemType[mySize])
    {
        int k;
            // copy elements
        for(k = 0; k < mySize; k++){
            myList[k] = vec.myList[k];
        }
    }
    
    template <class itemType>
    apvector<itemType>::~apvector ()
    // postcondition: vector is destroyed     
    {
        delete [] myList;
    }
    
    template <class itemType>
    const apvector<itemType> &
    apvector<itemType>::operator = (const apvector<itemType> & rhs)
    // postcondition: normal assignment via copying has been performed;
    //                if vector and rhs were different sizes, vector
    //                has been resized to  match the size of rhs
    {
        if (this != &rhs)                           // don't assign to self!
        {
            delete [] myList;                       // get rid of old storage
            mySize = rhs.length();
            myList = new itemType [mySize];         // allocate new storage
    
                // copy rhs
            int k;
            for(k=0; k < mySize; k++)
            {
                myList[k] = rhs.myList[k];
            }
        }
        return *this;                               // permit a = b = c = d
    }
    
    template <class itemType>
    int apvector<itemType>::length() const
    // postcondition: returns vector's size (number of memory cells 
    //                allocated for vector)
    {
        return mySize; 
    }
    
    template <class itemType>
    itemType & apvector<itemType>::operator [] (int k)
    // description: range-checked indexing, returning kth item
    // precondition: 0 <= k < length()
    // postcondition: returns the kth item     
    {
    
        if (k < 0 || mySize <= k)
        {
            cerr << "Illegal vector index: " << k << " max index = ";
            cerr << (mySize-1) << endl;
            abort();
        }
        return myList[k]; 
    }
    
    template <class itemType>
    const itemType & apvector<itemType>::operator [] (int k) const
    // safe indexing, returning const reference to avoid modification
    // precondition: 0 <= index < length
    // postcondition: return index-th item
    // exception: aborts if index is out-of-bounds
    {
        if (k < 0 || mySize <= k)
        {
            cerr << "Illegal vector index: " << k << " max index = ";
            cerr << (mySize-1) << endl;
            abort();
        }
        return myList[k]; 
    }
    
    template <class itemType>
    void apvector<itemType>::resize(int newSize)
    // description:  resizes the vector to newSize elements
    // precondition: the current capacity of vector is length(); newSize >= 0
    // postcondition: the current capacity of vector is newSize; for each k
    //                such that 0 <= k <= min(length, newSize), vector[k]
    //                is a copy of the original; other elements of vector are 
    //                initialized using the 0-argument itemType constructor
    //                Note: if newSize < length, elements may be lost
    {
        int k;        
        int numToCopy = newSize < mySize ? newSize : mySize;
    
             // allocate new storage and copy element into new storage
    
        itemType * newList = new itemType[newSize];
        for(k=0; k < numToCopy; k++)
        {
            newList[k] = myList[k];
        }
        delete [] myList;                      // de-allocate old storage
        mySize = newSize;                      // assign new storage/size
        myList = newList;
    }
    

    @edit

      

    @bigown - What is its advantage over Vector?

    • The class checks the subroutines at runtime to ensure that always stay within the limit, thus avoiding bugs of out of bounds .
    • The size of the apvector is stored in Vector , thus eliminating need an extra variable.
    • An apvector can be copied to another using the assignment.
    • When the apvector is declared, the compiler allocates only one space in the stack with its size and a pointer to the actual array, because the real array is dynamically allocated in free memory.
    07.06.2016 / 16:49