C++程序:向数组中添加一个元素

王林
王林 转载
2023-08-25 22:29:21 1654浏览

C++程序:向数组中添加一个元素

数组是一种线性顺序数据结构,用于在连续的内存位置中保存同质数据。与其他数据结构一样,数组也必须具备以某种有效方式插入、删除、遍历和更新元素的功能。在 C++ 中,我们的数组是静态的。 C++ 中还提供了一些动态数组结构。对于静态数组,该数组内可能存储 Z 个元素。到目前为止,我们已经有 n 个元素了。在本文中,我们将了解如何在 C++ 中在数组末尾插入元素(也称为追加元素)。

通过示例理解概念

‘this’关键字的使用方式如下

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

在上面的示例中,假设我们有一个数组 A,其中最多可以容纳 50 个元素。所以,Z 的值为 50。现在首先考虑一下,其中有 9 个元素。因此,数组的大小 n 为 9。要在数组末尾插入另一个元素,在我们的示例中为 23。该元素将被放置在末尾,并且 A 中的元素数量将增加 1。因此 n 变为 10。由于我们是在末尾插入,所以过程很简单。我们可以简单地在所有元素之后添加新元素,而无需更改数组中任何现有元素的位置。现在让我们看看该算法以及 C++ 实现代码,以便清楚地理解。

算法

  • 以数组 A 作为输入,元素数量 n 作为输入,以及将插入到 A 中的元素 e

  • 如果n是

    • A[ n ] = e

  • 结束如果

  • 将 n 增加为 n := n + 1

  • 返回数组 A 和新大小 n

示例

#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 insertAtEnd( int arr[], int &n, int e ){
   if( n < Z ) {
      arr[ n ] = e;
   }
   n = n + 1;
}

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

输出

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

使用向量附加元素

向量是 C++ STL 附带的动态数据结构。我们也可以获得类似的功能,例如向量中的数组。在向量内部,我们使用 push_back() 函数获得在末尾插入的功能。 push_back 函数将新元素作为参数,并将该元素插入到给定向量的末尾。该算法很简单。我们不需要做任何特殊的事情,只需通过传递我们要插入的新元素来调用给定向量对象的函数即可。我们直接看C++的实现。

示例

#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> insertAtEnd( vector<int> A, int e ){
   A.push_back( e );
   return A;
}

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 end:" << endl;
   A = insertAtEnd( A, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A );
   
   cout << "Inserting 225 at the end:" << endl;
   A = insertAtEnd( 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 end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 
Inserting 225 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 225,

结论

数组是连续保存同质数据的最简单的数据结构之一。数组是数据结构。与其他数据结构一样,我们也可以轻松地插入、删除、更新和遍历数组元素。在本文中,我们看到了两种在末尾插入元素的方法,换句话说,将元素追加到数组中。在第一个方法中,我们在 C++ 中使用静态数组。由于我们的目标是结束位置,因此不需要移动数组中的任何元素,只需在最后一个索引处添加一个新元素,并增加总项目计数参数以供进一步使用。在第二种情况下,我们使用向量。向量就像 C++ 中的普通数组,但它们本质上是动态的。它会在需要时自动更新其总大小。 C++ STL 支持向量,向量有一个名为 push_back() 的特殊函数,用于在后面插入元素。但是,我们不能用这种简单直接的方法在开头添加元素。

以上就是C++程序:向数组中添加一个元素的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除