动态数组的大小与提交的值不匹配

Size of dynamic array does not match the value submitted

本文关键字:提交 不匹配 数组 动态      更新时间:2023-10-16

我正在学习动态记忆,但有些地方不太对劲。我有一个函数,它接受一个数字作为输入,并应该生成一个该大小的数组。

class Doctor {
public:
    Doctor();
    void fillOut();
    void listPatients();
    void patientReset();
    ~Doctor();
private:
    string name;
    int numPatients;
    string *patientList;
};
Doctor::Doctor() {
    name = "";
    numPatients = 0;
    patientList = new string[numPatients];
}

(第三个代码块中的最相关代码)。

void Doctor::fillOut() 
{
    string buffer = "";
    string buffer2 = "";
    size_t found;
    bool valid = false;
    int numP = 0;
    int tester = 0;
    bool validNum = false;
    while(!valid) 
    {
        cout << "Enter doctor name: ";
        getline(cin, buffer);
        found = buffer.find_first_of("1234567890!@#$%^&*()-=_+/<>?;':][");
        if(string::npos == found) 
        {
            name = buffer;
            valid = true;
        }
    }
    while (!validNum) 
    {
        cout << "nEnter number of patients: ";
        buffer = "";
        getline(cin, buffer);
        buffer2 = buffer;
        stringstream ss(buffer);
        if(ss >> tester) 
        {
            stringstream ss2(buffer2);
            ss2 >> numP;
            validNum = true;
        }
        else 
        {
            cout << "Not a number. Please try again." << endl;
        }
    }
    patientList = new string[numP];
    cout << patientList->size() << endl;
    for(int i = 0; i < (numP + 0); i++) 
    {
        valid = false;
        while(!valid) 
        {
            cout << "nEnter patient " << (i + 1) << ": ";
            getline(cin,buffer);
            found = buffer.find_first_of("1234567890!@#$%^&*()-=_+,./<>?;':][");
            if(string::npos == found) 
            {
                *(patientList + i - 0) = buffer;
                //patientList[i-1] = buffer;
                valid = true;
            }
            else 
            {
                valid = false;
            }
        }
    }
}

然后我尝试显示列表的内容。

void Doctor::listPatients() 
{
    cout << "size: " << patientList->size() << endl;
    cout << "nDoctor: " << name << endl;
    for(int i = 0; i < (patientList->size() - 1); i++) 
    {
        cout << "Patient " << (i+1) << ": " << patientList[i] << endl;
    }
    cout << "end patients" << endl;
}

但由于某种原因,我提交的数字并不是数组的大小。例如,在fillOut()中,我有输出大小的函数。它每次都将大小输出为0。然后在listPatients()中,我有一些东西可以再次打印尺寸,以验证我做得对。如果我最初输入3,它在这个函数中有输出5。

我完全感到困惑。

patientList->size()相当于patientList[0].size(),即patientList数组中初始字符串的长度。在刚刚分配数组的点上,结果总是为零;在其他情况下,它是第一个字符串的长度。

在C++中制作容器的一种优选方式是std::vectorstd::array。在您的情况下,使用std::vector更合适,因为您的代码动态地分配patientList

好的,我发现了你的问题。您正在声明一个字符串数组。但是,原始数组实际上只是指向数组内容的指针。当您调用->size时,它会取消引用该指针,该指针指向数组中的第一个字符串,并告诉该字符串的大小。

如果您想要一个真正知道其大小的数组,请使用std::array。

好的,所以您将patientList声明为字符串*,但当您对此调用size()时,实际上是在您创建的数组中的第一个项上调用它。编译代码的唯一原因是字符串有一个size()函数,因为这是数组中包含的类型,所以您可以忽略它,因为patientList实际上指向数组开头的项(字符串)。通常,数组没有size()函数。

你会遇到的另一个问题是在你的循环中

for(int i = 0; i < (patientList->size() - 1); i++) {
                 ^
                 ^  
    cout << "Patient " << (i+1) << ": " << patientList[i] << endl;
}

这将导致您的循环在patientList结束前终止一个。你应该使用

for(int i = 0; i < patientList->size(); i++) {                                      
    cout << "Patient " << (i+1) << ": " << patientList[i] << endl;
}

你最好在这里使用std::vector来放入字符串

   if(string::npos == found) {
        *(patientList + i - 0) = buffer;
        //patientList[i-1] = buffer;
        valid = true;
    }
    else {
        valid = false;
    }

因为只有当if语句为true时,您才会向数组赋值——当它为false时,数组中的索引将保持未初始化状态。老实说,我会从头开始,使用字符串向量。