使用动态内存分配将元素添加到数组中
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 个元素: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;
}
相关文章:
- 如何将元素添加到数组的线程安全函数?
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 添加到数组指针
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 将数组的元素添加到链表中
- 如何在C++中将元素添加到非固定大小的数组中?
- 将在堆栈上声明的元素添加到静态数组
- const_cast静态数组以添加恒常性
- 在 char* 数组中添加两个索引
- c++ 如何在数组的开头添加值并为其编制索引?
- 检查数组中是否有字符串中的值,如果没有,则添加它
- 添加数组特定列的元素
- 添加数组元素:需要左键作为赋值的左操作数
- 添加数组所有元素的 SSE 指令
- 如何添加数组的偶数和奇数之和
- 向std::multimap添加数组值
- 用内部类函数递归地添加数组数
- 使用自增操作符添加数组元素
- 创建一个模板函数,如果数据类型不同,则添加数组的元素