gpt4 book ai didi

c++ - 如何在指定大小的数组中移动和插入元素

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:41 27 4
gpt4 key购买 nike

我是 C++ 的初学者。对于我的作业,我应该将一个元素插入到特定索引处的数组中。我尝试这样做,我认为我使用的算法是正确的,但我的数组不会移动。

这是我的代码:

 #include <iostream>
using namespace std;

const int CAPACITY = 10;

// Declaration function insertAtIndex
void insertAtIndex(int a[], int elements, int value, int index);

#include "Testing.hxx"

int main( )
{
testCases();

cout << endl;
system("Pause");
return 0;
}


void insertAtIndex(int a[], int elements, int value, int index);
{
if (elements == CAPACITY)
cerr << "Array is full. Cannot insert another element." << endl;
else if (index > CAPACITY)
cerr << "The array connot have more than 10 elements." << endl;

else if (index > elements)
cerr << "You can only insert continguous elements in the array." << endl;

else
{
elements++;
if (index == 0)
a[0] = value;
else
for (int i = elements; i >= index; i--)
{
a[i + 1] = a[i];
}
//insert value at index
a[index] = value;

}

出于某种原因,我的代码将在索引处插入值,但不会移动数组中的元素。当它达到数组中元素的数量时,它不会显示任何内容。如果数组为空,它也不会插入它 例如:当我想在索引 0 处插入 10 时,它只是说数组中没有元素。

这是我的测试用例:

*初始数组:数组中没有元素。在 idx 0 处插入 10...修改后的数组:数组中没有元素。

初始数组:1在 idx 0 处插入 20...修改后的数组:20

初始数组:3在 idx 1 处插入 30...修改后的数组:3

初始数组:5 3在 idx 1 处插入 40...修改后的数组:5 40

初始数组:5 3 1 7在 idx 4 处插入 60...修改后的数组:5 3 1 7

初始数组:8 4 2 6 7 8 2在 idx 7 处插入 80...修改后的数组:8 4 2 6 7 8 2

初始数组:9 8 5 6 3 2 1 4
在 idx 8 处插入 90...修改后的数组:9 8 5 6 3 2 1 4

这是我想在成功案例中实现的目标:

初始数组:9 8 5 6 3 2 1 4

在 idx 8 处插入 90...

修改后的数组:9 8 5 6 3 2 1 4 90

这是测试代码:

#ifndef TESTING_H
#define TESTING_H

#include <iostream>
#include <vector>
using namespace std;

vector< vector<int>> v = { { },
{ 1 },
{ 3 },
{ 5, 3 },
{ 7, 4 },
{ 5, 3, 1, 7 },
{ 4, 2, 7, 4 },
{ 8, 4, 2, 6, 7, 8, 2 },
{ 9, 8, 5, 6, 3, 2, 1, 4 },
{ 1, 6, 4, 8, 9, 0, 7, 5, 2, 3 },
{ 4, 6, 2},
{ 0, 1, 2, 3, 4, 5, 6 ,7 ,8, 9 } };

int indices[] = { 0, 0, 1, 1, 2, 4, 5, 7, 8, 10, 20, 5 };

void printArray(const int a[], int numOfElements)
{
if (numOfElements == 0)
cout << "No elements in the array.";
else
for (int i = 0; i < numOfElements; ++i)
cout << a[i] << " ";
}

void testing(int i)
{
int a[CAPACITY];
int numOfElem = static_cast<int>(v[i].size());
for (int j = 0; j < numOfElem; ++j)
a[j] = v[i].at(j);
cout << "Initial Array: ";
printArray(a, numOfElem);
cout << endl;
int elem = (i + 1) * 10;
cout << "Insert " << elem << " at idx " << indices[i] << "...\nModified array: ";
insertAtIndex(a, numOfElem, elem, indices[i]);
printArray(a, numOfElem);
cout << "\n--------------------------------------------------------\n";
}
void testCases()
{
int vectorSize = static_cast<int>(v.size());

for (int i = 0; i < vectorSize; ++i)
{
testing(i);
}
}

#endif

最佳答案

此代码的一个问题是您递增 elements,然后将其用作循环索引 i 的初始值,然后写入 a[i +1]。但是,您确实希望从索引 elements(未增加的值)开始,这是扩大数组中的最后一个有效索引。如果您在容量的一个元素内,此错误将导致未定义的行为。

另一个是您只增加名为 elements 的函数参数,这是一个临时拷贝。它不会被传回。因此,您的数组永远不会增长。如果您将函数参数声明为 const,编译器将为您捕获此错误。

我通常鼓励您在计算新值时声明一个新的 const 变量,而不是更改现有变量的值。 (规则的一个异常(exception):递增或递减循环计数器。)要么你将再次需要旧值,在这种情况下覆盖它是编译器无法捕获的错误,或者你不会,在这种情况下优化器的依赖分析应该可以弄清楚它可以丢弃它。您也不太可能犯这样的错误,在这种情况下,您递增 elements 然后再次向其添加 1,因为您忘记了更改它。

您没有向我们展示您的testcases() 函数,因此我既无法编译这段代码也无法查看缺失的部分。 Please provide a MCVE ,因为这样可以为您提供帮助。

这是学习在调试器中运行程序、插入断点和单步执行循环的好机会。当您遇到此类错误时,这是​​一个很好的起点。

关于c++ - 如何在指定大小的数组中移动和插入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48728045/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com