首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态数组容器类是否需要isFull()函数?

动态数组容器类是否需要isFull()函数?
EN

Stack Overflow用户
提问于 2020-03-16 09:16:23
回答 2查看 147关注 0票数 0

我遇到了一点麻烦;我使用VS中的调试器来检查我的代码。我意识到,当我在main()中调用insertBack()函数时,不会插入元素,因为不满足条件if (!isFull) --返回false,导致插入不发生。我尝试删除条件,但遇到一些错误,因为我的代码试图将一个数字插入数组的无效部分。在经历这一切的过程中,我开始问自己,既然动态数组可以调整大小,那么isFull()函数是必需的吗?但是,如果是这样的话,它怎么会满呢?我在cpprefrence上查看了一下向量,没有找到isFull()成员函数。

代码语言:javascript
复制
#include <iostream>

template<typename T>
class container
{
    template <typename T2>
    friend std::ostream& operator<<(std::ostream& out, const container<T2> &cobj);
    // Postcondition: contents of the container object cobj is displayed
public:
    container();
    // Postcondition: an empty container object is created with data members arr set to NULL, n set to -1 and Capacity set to 0
    ~container();
    // Destructor; required as one of the Big-3 (or Big(5) because of the presence of a pointer data member. Default version results in 
    //    memory leak!
    // Postcondition: dynamic memory pointed to by arr has been release back to the “heap” and arr set to NULL or nullptr
    //                 In order to see the action, message "destructor called and dynamic memory released!" is displayed
    bool isEmpty() const;
    // Postcondition: returns true is nothing is stored; returns false otherwise
    bool isFull() const;
    // Postcondition: returns true if arr array is filled to capacity; returns false otherwise
    int size() const;
    // Postcondition: returns the size or the number of elements (values) currently stored in the container
    int capacity() const;
    // Postcondition: returns the current storage capacity of the container
    bool insertBack(const T& val);
    // Postcondition: if container is not full, newVal is inserted at the end of the array; 
    //                otherwise, double the current capacity followed by the insertion
    bool deleteBack();
    //  Precondition: The array must not be empty
    // Postcondition: the last element stored in the array is removed! size of the container is decremented by 1, capacity unchanged
    void clear();
    // Postcondition: all elements in arr of calling container object are cleared and the dynamic memory is released back to “heap” 
private:
    void allocate(T* &temp);
    // Postcondition: if Capacity = 0, allocate a single location; otherwise the current capacity is doubled
    T *arr;
    int Capacity;   // Note: Capital 'C' as capacity is used as a function name
    int n;          // size or actual # of values currently stored in the container; n <= SIZE
};

template<typename T2>
std::ostream& operator<<(std::ostream& out, const container<T2> &cobj)
{
    std::cout << "Currently it contains " << cobj.size() << " value(s)" << std::endl
        << "Container storage capacity = " << cobj.capacity() << std::endl
        << "The contents of the container:" << std::endl;

    if (cobj.isEmpty())
    {
        std::cout << "*** Container is currently empty!" << std::endl;
    }
    else
    {
        for (int i=0; i<cobj.size(); ++i)
        {
            std::cout << cobj.arr[i];
        }
    }

    return out;
}

template<typename T>
container<T>::container()
{
    arr = nullptr;
    Capacity = 0;
    n = 0;
}

template<typename T>
container<T>::~container()
{
    delete arr;
    arr = nullptr;
    std::cout << "Destructor called! (this line is normally not displayed)" << std::endl;
}

template<typename T>
bool container<T>::isEmpty() const
{
    return n==0;
}

template<typename T>
bool container<T>::isFull() const
{
    return n==Capacity;
}

template<typename T>
int container<T>::capacity() const
{
    return Capacity;
}

template<typename T>
int container<T>::size() const
{
    return n;
}

template<typename T>
bool container<T>::insertBack(const T& val)
{
    if (!isFull())
    {
        n++;
        arr[n-1] = val;
        return true;
    }
    else
    {
        return false;
    }
}

template<typename T>
bool container<T>::deleteBack()
{
    if (!isEmpty())
    {
        n--;
        return true;
    }
    else
    {
        return false;
    }
}

template<typename T>
void container<T>::clear()
{
    if (!isEmpty())
    {
        n = 0;
        return true;
    }
    else
    {
        return false;
    }
}

template<typename T>
void container<T>::allocate(T* &temp)
{
    if (Capacity==0)
    {
        temp = new T;
    }
    else
    {
        return Capacity*2;
    }
}

int main()
{
    container<int> a1;
    std::cout << a1 << std::endl; 
    std::cout << "Currently, the container object contains 0 element(s) or 0 value(s)" << std::endl;

    std::cout << "\nWe now insert 3 values at the back of the array, one at a time:" << std::endl;

    const int num = 3;
    for (int i=0, c=0; i<=num; ++i, c+=10)
    {
        a1.insertBack(c);
    }

    std::cout << a1;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-16 14:21:32

我认为使用isFull方法是没有意义的,因为您的动态容器在容量上没有限制。相反,您可以使用sizecapacity方法来跟踪容器的状态。

票数 1
EN

Stack Overflow用户

发布于 2020-03-16 14:26:49

如果您想实现一个向量,并且想要检查大小是否小于或等于容量,然后决定是否调整它的大小,您可以将size > = capacity包装为私有的isFull()函数。但我认为将其公之于众是没有意义的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60699277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档