上一个功能没有显示正确的输出

时间:2014-12-07 19:16:41

标签: c++ arrays vector

我没有得到我编码的程序的预期输出,我知道最后一个函数的实现是不正确的。我不知道如何实现它,以便它将最后一个条目返回到数组中,并且不会删除它。我的第二个问题是关于pop_back函数,它应该删除推入向量的最后一个条目并将其减少一个,如果它是空的则什么都不做。它现在的方式只是将向量减少一个。感谢您的帮助。

驱动程序

#include <iostream>
#include "vectorHeader.h"

using namespace std;

int main()
{
const char START = 'A';
const int MAX = 12;

// create a vector of doubles
myVector<char> vectD;

// push some values into the vector
for (int i = 0; i < MAX; i++)
{
    vectD.push_back(START + i);
}

// remove the last element
vectD.pop_back();

// add another value
vectD.push_back('Z');

// test memory management
myVector<char> vectD2 = vectD;
// display the contents
cout << "\n[";
for (int i = 0; i < vectD2.size() - 1; i++)
{
    cout << vectD2[i] << ", ";
}

cout << "..., " << vectD2.last() << "]\n";


system("PAUSE");
return 0;

}

标题

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
#include <array>


//Declaring constant
const int VECTOR_CAP = 2;

template <class T>
class myVector
{
private:
    //Setting data members
    T* vectorData;
    int cap;
    int numElements;

public:
    //Default constructor
    //Purpose: Creates a vector
    //Parameters: None
    //Returns: None
    myVector();

    //Parameterized constructor
    //Purpose: Creates a vector capacity of n
    //Parameters: None
    //Returns: None
    myVector(const T&);

//Copy Constructor
//Purpose: Copy data into vector
//Parameters: myVector object
//Returns: None
myVector(const myVector& copy)
{
    numElements = copy.numElements;
    vectorData = new T [numElements];
    for (int i = 0; i < numElements; i++)
    {
        this->vectorData[i] = copy.vectorData[i];
    }
}

//Destructor
//Purpose:Deletes any dynamically allocated storage
//Parameters: None
//Returns: None
~myVector();

//Size function
//Purpose: returns the size of your vector
//Parameters: None
//Returns: The size of your vector as an integer
int size() const;

//Capacity function
//Purpose: Returns the capacity of the vector
//Parameters: None
//Returns: Maximum value that your vector can hold
int capacity() const;

//Clear function
//Purpose: Deletes all of the elements from the vector and resets its size to
// zero and its capacity to two; thus becoming empty
//Parameters: None
//Returns: None
void clear();

//push_back function
//Purpose: Adds the integer value n to the end of the vector
//Parameters: Takes a integer to be placed in the vector
//Returns: None
void push_back(const T& n)
{
    //If statement to handle if array is full 
    if (numElements == cap)
    {
        //Doubling the capacity 
        cap = cap * VECTOR_CAP;
        //Allocating new array 
        T* newVectorData = new T[cap];
        //Copying data
        for (int i = 0; i < numElements; i++) newVectorData[i] = vectorData[i];
        //Deleting previous data
        delete[] vectorData;
        //Pointing to new data
        vectorData = newVectorData;
    }
    //Storing data
    vectorData[numElements++] = n;
}

//at function
//Purpose: Returns the value of the element at position n in the vector
//Parameters: None
//Returns: Returns your current place with the vector
T& at(std::size_t);

//assignment
//Purpose: Overload the = operator
//Parameters: The two myVector objects we want to assign
    //Returns: The assignment
    myVector operator=(const myVector&);

    void pop_back();

    int last();

    T& operator[](std::size_t);

};

//Independant Functions

template <typename T>
myVector<T>::myVector()
{
    //Setting the cap
    cap = VECTOR_CAP;
    //Creating the array
    vectorData = new T[cap];
    //Initializing the value
    numElements = 0;
}

template <typename T>
myVector<T>::~myVector()
{
    cap = 0;
    //Delete array elements
    delete[] vectorData;
    //Allocate vectorData
    vectorData = NULL;
}

template <typename T>
int myVector<T>::size() const
{
    return numElements;
}

template <typename T>
void myVector<T>::pop_back() 
{
    numElements--;
}

template <typename T>
int myVector<T>::last() 
{
    return cap;
}


template <typename T>
int myVector<T>::capacity() const
{
    return cap;
}

template <typename T>
T& myVector<T>::at(std::size_t n)
{
    return vectorData[n];
}

template <typename T>
T& myVector<T>::operator[](std::size_t n)
{
    return vectorData[n];
}

template <typename T>
myVector<T> myVector<T>::operator=(const myVector& rho)
{
    //Test for assingment
    if (this == &rho)
    {
        return *this;
    }

    //Delete lho
    delete[] this->vectorData;

    //Creating new array to fit rho data
    cap = rho.cap;
    this->vectorData = new int[cap];

    //Copying data
    for (int i = 0; i < numElements; i++)
    {
        this->vectorData[i] = rho.vectorData[i];
    }

    //Returning myVector object
    return *this;
}    


template <typename T>
std::ostream& operator<<(std::ostream& out, const myVector<T>& rho)
{
    for (int n = 0; n < rho.size(); n++)
    {
        out << rho.at(n);
    }
    return out;
}

1 个答案:

答案 0 :(得分:1)

last函数应如下所示:

template <typename T>
T myVector<T>::last() 
{
    return vectorData[numElements - 1];
}

从向量中返回元素的函数应该具有向量元素类型的返回类型,例如T

coutcharint有不同的重载。对于char,它打印所提供代码的ASCII字符,对于int,它返回代码本身。因此,如果您的最后一个元素为Z并且您返回int,则会打印Z的{​​{1}}的ASCII代码。

试试这个让我意识到我在说什么:

90

至于cout << 'Z' << endl; cout << (char) 'Z' << endl; // tautological cast as 'Z' is char cout << (int) 'Z' << endl; cout << 90 << endl; cout << (int) 90 << endl; // tautological cast as 90 is int cout << (char) 90 << endl; ,你所要做的就是:

pop_back

原因:没有删除内存这样的东西。内存单元格总是有一个值(可以是你设置的值,0,1或垃圾)。您所要做的就是将其标记为可用(或未使用)。这就是缩小if (numElements > 0) numElements--; 时的行为。

如果您想更进一步,可以执行与numElements相反的操作,即将整个向量重定位到较小的已分配缓冲区。但是不建议这样做,因为这些操作(分配,复制)是昂贵的,你可以没有它们(不像push_back,你必须得到更大的尺寸)