2014-12-07 83 views
1

我沒有得到我編碼的程序的預期輸出,我知道最後一個函數的實現是不正確的。我不知道如何實現這個,以便它將最後一個條目返回到數組中,並且不會將其刪除。第二個問題是在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; 
} 
+0

um ...''return vectorData [numElements - 1];' ... – bolov 2014-12-07 19:21:32

+0

如果我是你的助理老師,你會很難讓我相信你能夠改變你的矢量容量(處理2個指針:初始化,複製,交換),你不會知道如何返回數組中的最後一個元素。 – bolov 2014-12-07 19:26:16

+0

這仍然沒有給我正確的輸出。它應該是'Z',但是這是'90' – DEnumber50 2014-12-07 19:27:51

回答

1

last函數應該是這樣的:

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

從向量返回元素應該有返回類型的函數向量元素的類型,即在您的ca中的T SE。

cout對於charint有不同的過載。對於char它打印所提供的代碼的ASCII字符,因爲它返回代碼本身int。因此,如果您的最後一個元素是Z並且您返回int那麼它將打印Z的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所有你需要做的是:

if (numElements > 0) 
    numElements--; 

原因:有沒有這樣的事情作爲刪除內存。內存單元總是有一個值(不管是你設置的,0,1還是垃圾)。您只需將其標記爲可用(或未使用)即可。這就是你在收縮numElements時所做的。

如果您想更進一步,您可以做與push_back相反的操作,即將整個矢量重新定位到更小的已分配緩衝區。但不建議這樣做,因爲這些操作(分配,複製)是昂貴的,你可以不用它們(不像pus_back,你必須得到更大的尺寸)