尝试将元素添加到动态数组

发布于 2025-02-01 12:54:18 字数 519 浏览 3 评论 0原文

我尝试将一个从类类型添加到一个空的动态数组中,但是什么也不会发生,也没有在添加一个元素后更改计数器。 这是功能

void Bank::addCustomer(const Customer& newCustomer) {

    Customer* temp = new Customer[getCustomerCount()+1]; 

    for (int i = 0; i < getCustomerCount() + 1 ; ++i) {
        if (getCustomerCount() != 0) {
            temp[i] = customers[i];
        }
    }

    ++customerCount;
    setCustomerCount(customerCount);
    delete[] customers; 
    customers = temp;

    customers[customerCount] = newCustomer; 
    //log
}

i've tried to add an element from class type to an empty dynamic array, but nothing happens, nor it changes the counter after adding one element.
This is the function

void Bank::addCustomer(const Customer& newCustomer) {

    Customer* temp = new Customer[getCustomerCount()+1]; 

    for (int i = 0; i < getCustomerCount() + 1 ; ++i) {
        if (getCustomerCount() != 0) {
            temp[i] = customers[i];
        }
    }

    ++customerCount;
    setCustomerCount(customerCount);
    delete[] customers; 
    customers = temp;

    customers[customerCount] = newCustomer; 
    //log
}

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

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

发布评论

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

评论(3

翻了热茶 2025-02-08 12:54:18

如果您的循环不为空,则超过旧数组的边界。 newcustomer的分配超出了新数组的界限。

而是尝试一下:

void Bank::addCustomer(const Customer& newCustomer) {

    int count = getCustomerCount(); 
    Customer* temp = new Customer[count + 1];

    for (int i = 0; i < count; ++i) {
        temp[i] = customers[i];
    }
    temp[count] = newCustomer; 

    delete[] customers; 
    customers = temp;

    ++customerCount;

    //log
}

Your loop is exceeding the bounds of the old array if it is not empty. And your assignment of the newCustomer is exceeding the bounds of the new array.

Try this instead:

void Bank::addCustomer(const Customer& newCustomer) {

    int count = getCustomerCount(); 
    Customer* temp = new Customer[count + 1];

    for (int i = 0; i < count; ++i) {
        temp[i] = customers[i];
    }
    temp[count] = newCustomer; 

    delete[] customers; 
    customers = temp;

    ++customerCount;

    //log
}
┈┾☆殇 2025-02-08 12:54:18

我猜这就是您的意思:

void addCustomer(const Customer& newCustomer) {
    int old_customer_count = getCustomerCount();
    int new_customer_count = old_customer_count + 1;

    Customer* temp = new Customer[new_customer_count];
    for (int i = 0; i < new_customer_count; ++i) {
        temp[i] = customers[i];
    }

    setCustomerCount(new_customer_count);
    delete[] customers;
    customers = temp;
    customers[old_customer_count] = newCustomer;
}

但是正如某人所说,在实际代码中,您应该为客户收集std :: vector

I'm guessing this is what you mean:

void addCustomer(const Customer& newCustomer) {
    int old_customer_count = getCustomerCount();
    int new_customer_count = old_customer_count + 1;

    Customer* temp = new Customer[new_customer_count];
    for (int i = 0; i < new_customer_count; ++i) {
        temp[i] = customers[i];
    }

    setCustomerCount(new_customer_count);
    delete[] customers;
    customers = temp;
    customers[old_customer_count] = newCustomer;
}

but as someone said, in real code you should an std::vector for the customers collection.

纸伞微斜 2025-02-08 12:54:18

尝试使用向量进行动态阵列。阿里不能动态。 push_back()函数可用于将元素添加到向量。

Try using vector for dynamic array. arry cann't be dynamic. push_back() function can be used for adding element to vector.

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