C++程序:在数组开头添加元素


数组和数据结构使得能够在多个内存位置存储同质(**相同**)数据成为可能。使用数组的主要好处是我们可以使用索引参数从任何我们想要的地方检索它们。由于数据必须依次插入和删除,因此这种数据结构成为线性的。我们只需要在方括号内放置该元素的索引或位置号即可从数组中检索它。在本文中,我们将使用数组 A 和另一个元素 e。我们将在 C++ 中将 e 插入到 A 的起始位置。

通过示例理解概念

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
After inserting 23 at the end, the array will look like this:
[23, 10, 14, 65, 85, 96, 12, 35, 74, 69]

在上面的示例中,我们有一个包含九个元素的数组 A。我们将另一个元素 23 插入到数组 A 的开头。结果数组包含所有元素,以及开头位置的 23。要在开头插入元素,我们必须将所有元素向右移动一个位置,然后第一个槽将为空,我们将新元素放在该位置。让我们看看算法以获得清晰的理解。

算法

  • 获取数组 A 和元素 e

  • 如果数组 A 有足够的空间插入元素 e,则

    • 对于 i 从范围 n - 1 到 0,执行

      • A[ i + 1 ] = A[ i ]

    • 结束循环

    • A[ 0 ] = e

    • 将 n 增加 1

  • 结束 if

  • 返回 A

示例

#include <iostream>
# define Z 50

using namespace std;

void displayArr(int arr[], int n){
   for( int i = 0; i < n; i++ ){
      cout << arr[ i ] << ", ";
   }
   cout << endl;
}
void insertAtBeginning( int A[], int &n, int e ){
   if( n + 1 < Z ) {
      for( int i = n - 1; i >= 0; i-- ) {
         A[ i + 1 ] = A[ i ];
      }
      A[ 0 ] = e;
      n = n + 1;
   } 
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Array before insertion: ";
   displayArr( A, n );
   
   cout << "Inserting 58 at the beginning:" << endl;
   insertAtBeginning( A, n, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A, n );
   
   cout << "Inserting 225 at the beginning:" << endl;
   insertAtBeginning( A, n, 225 );
   
   cout << "Array after insertion: ";
   displayArr( A, n );
}

输出

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,

使用向量在前面插入元素

向量是 C++ STL 附带的动态数据结构。我们也可以在向量中获得类似于数组的功能。但在向量中,我们只能在末尾或后面插入。没有直接的方法可以在开头插入。但是,我们可以像以前一样,将元素移动一个位置,然后在新元素插入到开头。或者,我们可以使用新元素创建一个另一个单元素向量,然后将它们连接起来。因此,结果向量将包含所有以前的元素以及开头的新元素。让我们看看算法和 C++ 实现。

算法

  • 获取数组 A 和元素 e

  • 创建一个空向量 B

  • 将 e 插入 B

  • A := 将 B 与 A 连接 (先 B 后 A)

  • 返回 A

示例

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
   for( int i = 0; i < v.size() ; i++ ){
      cout << v[ i ] << ", ";
   }
   cout << endl;
}

vector<int> insertAtBeginning( vector<int> A, int e ){
   vector<int> B( 1 );
   B[ 0 ] = e;
   B.insert( B.end(), A.begin(), A.end() );
   return B;
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};  
   cout << "Array before insertion: ";
   displayArr( A );
   
   cout << "Inserting 58 at the beginning:" << endl;
   A = insertAtBeginning( A, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A );
   
   cout << "Inserting 225 at the beginning:" << endl;
   A = insertAtBeginning( A, 225 );
   
   cout << "Array after insertion: ";
   displayArr( A );
}

输出

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,

结论

在本文中,我们已经了解了如何在数组的开头插入元素。我们在这里讨论了两种不同的解决方案。在第一个解决方案中使用了静态 C++ 数组,在第二个解决方案中使用了向量。向量没有直接在开头插入元素的方法。我们可以使用 push_back() 方法直接在末尾插入。为此,我们使用了一个技巧,我们创建了一个大小为 1 的数组,然后将新元素插入其中。之后将其与给定数组连接起来。我们可以使用列表来实现相同的效果。但是 C++ 列表具有 push_front() 方法,可以直接在前面插入元素。但是列表不是数组,它们是链表。

更新于:2022年12月13日

9K+ 浏览量

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.