使用动态内存分配将元素添加到数组中

Using dynamic memory allocation to add elements into an array

本文关键字:添加 数组 元素 动态 内存 分配      更新时间:2023-10-16

我应该写一些代码来要求用户输入一个数组(1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5(。

之后,我创建一个更大大小(两倍大小(的新数组,将所有元素从旧数组复制到新数组,然后要求用户继续在新数组中再输入 5 个元素:1.5 4.5 9.5 16.5 7.5 11.5,然后打印出最终数组(15 个元素(。

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
double* read_data(int& size) 
{
    int max = 10;         
    double* a = new double[max];  // allocated on heap
    size = 0;
    cout << "Enter the array: " << endl;
    while (cin >> a[size]) 
    {
        size++;
    }
    if (size >= max) 
    {
        double* temp = new double[max * 2];
        for (int i = 0; i < size; i++) 
        {
            temp[i] = a[i];       
        }
        delete[] a;             
        a = temp;   
        max = max * 2;  
    }
    return a;
}
int main () 
{
    int input1, input2, input3, input4, input5;
    int size = 0;
    double* arr = read_data(size);
    cout << "Please enter 5 more elements: " << endl;
    cin >> input1 >> input2 >> input3 >> input4 >> input5;
    arr[10] = input1;
    arr[11] = input2;
    arr[12] = input3;
    arr[13] = input4;
    arr[14] = input5;
    cout << "The final array is: " << endl;
    for (int i = 0; i < 15; i++)
    {
        cout << arr[i];
    }
    system("pause");
    return 0;
}

它不允许我再输入 5 个元素,我不知道为什么。请帮忙。

您正在等待输入流进入"false"状态,当向流输入文件字符结束时,这种情况最明显。 因此,在向终端输入代码时,如果您输入 -d,您应该会看到代码在 while 循环后移动到段上。

要解决此问题,您必须指定从用户那里获得的数组的限制,以免覆盖越界内存。

所以把你的代码改成这个

#include <iostream>
#include <string>
using namespace std;
double* read_data(int& size) 
{
    int max = 10;         
    double* a = new double[max];  // allocated on heap
    size = 0;
    cout << "Enter the array: " << endl;
    while (size < 10 && cin >> a[size]) 
    {
        size++;
    }
    if (size >= max) 
    {
        double* temp = new double[max * 2];
        for (int i = 0; i < size; i++) 
        {
            temp[i] = a[i];       
        }
        delete[] a;             
        a = temp;   
        max = max * 2;  
    }
    return a;
}
int main () 
{
    int input1, input2, input3, input4, input5;
    int size = 0;
    double* arr = read_data(size);
    cout << "Please enter 5 more elements: " << endl;
    cin >> input1 >> input2 >> input3 >> input4 >> input5;
    arr[10] = input1;
    arr[11] = input2;
    arr[12] = input3;
    arr[13] = input4;
    arr[14] = input5;
    cout << "The final array is: " << endl;
    for (int i = 0; i < 15; i++)
    {
        cout << arr[i] << ' ';
    } cout << endl;
    system("pause");
    return 0;
}

请注意,我如何在" cin"语句之前添加大小变量的检查,这称为短路(https://en.wikipedia.org/wiki/Short-circuit_evaluation(。 我自己不认为这种好风格,但我把它包括在这里是为了向您展示如何在 while 循环中使用输入语句,如果您不正确使用它可能会导致不良行为。

我还添加了一个cout << endl;来刷新流缓冲区,以便输出确实在pause之前进入屏幕


刚刚阅读了您在评论中所说的内容,我建议使用以下代码,以便在"q"退出时退出。

#include <iostream>
#include <string>
using namespace std;
double* read_data(int& size) 
{
    int max = 10;         
    double* a = new double[max];  // allocated on heap
    size = 0;
    cout << "Enter the array: " << endl;
    char character;
    while (size < 10 && cin >> character) 
    {
        if (character == 'q') break;
        a[size] = character - '0';
        size++;
    }
    if (size >= max) 
    {
        double* temp = new double[max * 2];
        for (int i = 0; i < size; i++) 
        {
            temp[i] = a[i];       
        }
        delete[] a;             
        a = temp;   
        max = max * 2;  
    }
    return a;
}
int main () 
{
    int size = 0;
    double* arr = read_data(size);
    cout << "Please enter 5 more elements: " << endl;
    for (int i = size; i < size + 5; ++i) {
        cin >> arr[i];
    } 
    // add 5 to the size
    size += 5;
    cout << "The final array is: " << endl;
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << ' ';
    } cout << endl;
    system("pause");
    return 0;
}