【问题标题】:Size of dynamic array does not match the value submitted动态数组的大小与提交的值不匹配
【发布时间】:2012-07-14 00:15:25
【问题描述】:

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

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。

我完全被迷惑了。

【问题讨论】:

  • 你是如何声明患者列表的?
  • string *patientList;...为什么?

标签: c++ arrays dynamic


【解决方案1】:

patientList-&gt;size() 行等同于patientList[0].size(),即patientList 数组中初始字符串的长度。在您刚刚分配数组时,结果始终为零;在其他情况下,它是第一个字符串的长度。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 大小在编译时不固定,所以std::vector会更好。
    【解决方案3】:

    好的,因此您将 patientList 声明为字符串*,但是当您对此调用 size() 时,实际上是在您创建的数组中的第一项上调用它。编译代码的唯一原因是因为 string 具有 size() 函数,并且由于这是数组中包含的类型,因此您可以摆脱它,因为 patientList 实际上指向数组开头的项目(字符串)。一般来说,数组没有 size() 函数。

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

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

    这将导致您的循环在患者列表结束之前终止。你应该使用

    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 时数组中的索引未初始化。老实说,我会从头开始并使用字符串向量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2021-01-31
      • 2014-07-21
      • 2011-01-03
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多