【问题标题】:Using dynamic memory allocation to add elements into an array使用动态内存分配将元素添加到数组中
【发布时间】:2016-02-09 04:03:26
【问题描述】:

我应该写一些代码让用户输入一个数组(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 个元素,我不知道为什么。 请帮忙。

【问题讨论】:

  • “它不允许我再输入 5 个元素”是什么意思?
  • 我的意思是在 cout
  • 它跳过下面的整个部分,输出只有10个元素
  • 它如何知道在第一个循环中何时停止阅读元素? while (cin &gt;&gt; a[size]) {size++;} 会读取尽可能多的元素,而不关心数组大小。
  • 嗯,我输入 q 退出; __ ;

标签: c++ arrays dynamic-memory-allocation


【解决方案1】:

您正在等待输入流进入“假”状态,这种情况在输入流的文件结束字符时最为明显。因此,在向终端输入代码时,如果您键入 -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 &lt;&lt; endl; 来刷新流缓冲区,以便输出在pause 之前进入屏幕


我刚刚阅读了您在 cmets 中所说的内容,我建议使用以下代码,以便在“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;
}

【讨论】:

  • 哦,我明白了,所以通过添加 size
  • 我投了赞成票,但我没有足够的代表(15 声望)。当我有 15 个代表时,我对答案的投票将显示:D
  • 如果输入是整数,一切都很好,但不知道为什么它不适用于浮点数'___'
  • 哦,是的,我知道为什么,将输入类型更改为 double =))
猜你喜欢
  • 2014-06-19
  • 2016-02-09
  • 2013-08-04
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2014-12-14
相关资源
最近更新 更多