What is the C++ vector

Asked

Viewed 91 times

1

The college professor asked to research the ordering scripts and perform tests with them, determining which is better in which situation.

I found the scripts, though on the site the guy does not show how to run, and he uses a apvector I don’t know where it comes from, so I can’t run the scripts.

That one apvector is a library? How 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
}
  • Opa, can put the answer giving example of how I import this class in my file ordenacoes.cpp, I am using Dev C++. So I can consider it as a valid answer. Thanks! : D

  • I can, but take a look first and if it really helps you, I don’t know about C and it was more research to help.

  • Opens a new project as a dynamic library and compiles the apvector.cpp, then import the dll to your project. if devc++ uses Makefile, just add the file apvector.cpp in compilation, if you want to try compiling by gcc, just use g++ apvector.cpp meuprojeto.cpp -o projeto.exe

1 answer

1


The class Apvector is class-based Vector, and all methods belonging to it are also part of Vector. It’s a template that you can specify what kind of collection it will work with.

Some features of Apvector:

  1. Are like arrays and its index starts from scratch
  2. They can be initialized to keep the same value in each index, providing a second argument for the constructor while the vector is defined.
  3. Can be resized
  4. Must be passed by reference

The following follows the implementation model of 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

@Maniero - What is her advantage over Vector?

  • Class checks 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 a Vector, thereby eliminating the need for an extra variable.
  • An apvector can be copied to another using the attribution.
  • When apvector is declared, the compiler allocates only one space in the stack with its size and a pointer to the actual matrix because the real array is dynamically allocated in free memory.

  • Good answer, I was left with only one question, what is her advantage in relation to Vector?

  • @bigown added the answer to your question.

  • I think this is the definition of Vector. It seems to me a class with no practical use whatsoever.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.