使用动态内存分配将元素添加到数组中
Using dynamic memory allocation to add elements into an array
我应该写一些代码来要求用户输入一个数组 (1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5)。
之后,我创建一个更大的新数组(两倍大小),将旧数组中的所有元素复制到新数组中,然后要求用户继续向新数组中输入 5 个元素array: 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;
}
我应该写一些代码来要求用户输入一个数组 (1.3 4 5.2 16.3 9.99 7.21 4.5 7.43 11.21 12.5)。
之后,我创建一个更大的新数组(两倍大小),将旧数组中的所有元素复制到新数组中,然后要求用户继续向新数组中输入 5 个元素array: 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;
}