动态阵列输出的问题

发布于 2025-01-27 08:28:15 字数 2179 浏览 1 评论 0原文

我正在使用用于动态数组的模板类,并且我的代码已经成功编译,但是在动态数组中,我对给定元素的获取函数似乎无法正常工作。每当我输出功能时,似乎都没有输出,但是这没有意义,因为我的大小功能正常,并且表明数组中确实有东西,我只是无法输出它。谁能帮忙?

#include <iostream>
using namespace std;

template <class T>
class Wrapper{
private:
    T* array;
    int size;
public:
    Wrapper(){
        array = NULL;
        size = 0;
    }
    int returnSize(){
        return size;
    }
    void addEntry(string input){
        if(returnSize()==0){
            array = new string[returnSize()+1];
            array[returnSize()] = input;
            size+=1;
        }
        else{
            T* newArray = new T[returnSize()+1];
            for(int i = 0; i<returnSize(); i++){
                newArray[i] = array[i];
            }
            newArray[returnSize()] = input;
            size+=1;
            string *array = new string[returnSize()];
            array = newArray;
        }
        
    }

    bool deleteEntry(string input){
        int mySize = returnSize();
        string* testArray = new string[mySize];
        for(int i = 0; i<mySize; i++){
            if(array[i]==input){
                return true;
                break;
            }
        }
        string*newArray = new string[mySize-1];
        for(int i = 0; i<mySize; i++){
            if(array[i]!=input){
                newArray[i+=1] = array[i];
        }
        }
        delete[]array;
        array = newArray;
        return true;
        
    }
    string getEntry(int input){
        if(input>size){
            return NULL;
        }
        return this->array[input];
    }
    void operator=(const Wrapper w){
        this->size = w.returnSize();
        string *newArray = new string[size];
        for(int i = 0; i<size; i++){
            newArray[i] = w.getEntry(i);
        }
        this->array = newArray;
    }
    ~Wrapper(){
        delete[] array;
    }

};

int main(){
    Wrapper<string> w;
    w.addEntry("sam");
    w.addEntry("Josh");
    w.addEntry("tom");
    cout<<"Here"<<endl;
    cout<<w.returnSize();
    for(int i = 0; i<<w.returnSize(); i++){
        cout<<w.getEntry(i);
    }
}

I'm working with a template class for dynamic arrays, and my code has compiled successfully, however my get function for a given element in the dynamic array does not seem to be working. Nothing seems to be outputted whenever I output the function, but this doesn't make sense, because my size function works fine, and shows that there is indeed something within the array, I just can't output it. Can anyone help?

#include <iostream>
using namespace std;

template <class T>
class Wrapper{
private:
    T* array;
    int size;
public:
    Wrapper(){
        array = NULL;
        size = 0;
    }
    int returnSize(){
        return size;
    }
    void addEntry(string input){
        if(returnSize()==0){
            array = new string[returnSize()+1];
            array[returnSize()] = input;
            size+=1;
        }
        else{
            T* newArray = new T[returnSize()+1];
            for(int i = 0; i<returnSize(); i++){
                newArray[i] = array[i];
            }
            newArray[returnSize()] = input;
            size+=1;
            string *array = new string[returnSize()];
            array = newArray;
        }
        
    }

    bool deleteEntry(string input){
        int mySize = returnSize();
        string* testArray = new string[mySize];
        for(int i = 0; i<mySize; i++){
            if(array[i]==input){
                return true;
                break;
            }
        }
        string*newArray = new string[mySize-1];
        for(int i = 0; i<mySize; i++){
            if(array[i]!=input){
                newArray[i+=1] = array[i];
        }
        }
        delete[]array;
        array = newArray;
        return true;
        
    }
    string getEntry(int input){
        if(input>size){
            return NULL;
        }
        return this->array[input];
    }
    void operator=(const Wrapper w){
        this->size = w.returnSize();
        string *newArray = new string[size];
        for(int i = 0; i<size; i++){
            newArray[i] = w.getEntry(i);
        }
        this->array = newArray;
    }
    ~Wrapper(){
        delete[] array;
    }

};

int main(){
    Wrapper<string> w;
    w.addEntry("sam");
    w.addEntry("Josh");
    w.addEntry("tom");
    cout<<"Here"<<endl;
    cout<<w.returnSize();
    for(int i = 0; i<<w.returnSize(); i++){
        cout<<w.getEntry(i);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2025-02-03 08:28:15

只是为了收集调查结果,如果您从Addentry删除此行,

           string *array = new string[returnSize()];

然后将循环的最终结果更改为:

    for(int i = 0; i < w.returnSize(); i++){
        cout<<w.getEntry(i)<<"\n";
    }

这是您所期望的。我自己运行。

Just to collect the findings, if you delete this line from addEntry:

           string *array = new string[returnSize()];

and change your final for loop to:

    for(int i = 0; i < w.returnSize(); i++){
        cout<<w.getEntry(i)<<"\n";
    }

then this does exactly what you expect. I've run it myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文