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

How to shift and insert an element in an array with a specified size

我是 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 变量,而不是更改现有变量的值。 (规则的一个例外:递增或递减循环计数器。)要么你将再次需要旧值,在这种情况下覆盖它是编译器无法捕获的错误,或者你不会,在这种情况下优化器的依赖分析应该可以弄清楚它可以丢弃它。你也不太可能犯这样的错误,你增加 elements 然后再次添加 1 因为你忘记了你改变了它。

您没有向我们展示您的 testcases() 函数,因此我无法编译此代码,也无法查看缺失的部分。 Please provide a MCVE,这样才能帮到你。

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