首页 > 后端开发 > C++ > C++程序将一个数组推入另一个数组中

C++程序将一个数组推入另一个数组中

WBOY
发布: 2023-09-04 13:37:06
转载
1374 人浏览过

C++程序将一个数组推入另一个数组中

A linear sequential data structure called an array is used to store homogeneous data in a series of memory regions. An array needs to have certain features to insert, delete, traverse, and update elements effectively, just like other data structures do. Our arrays in C++ are static. In addition, C++ offers a few dynamic array structures. There may be a maximum of Z elements that can be stored inside a static array. And there are currently n elements in it. In this article, we will see how to push the elements of one array inside another array in C++.

理解概念并举例说明

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
Another given array B = [56, 42, 15, 18, 20, 32]

Pushing the elements of B into A, signifies that A becomes:
A = [10, 14, 65, 85, 96, 12, 35, 74, 69, 56, 42, 15, 18, 20, 32]
登录后复制

在上面的示例中,很明显我们有两个数组A和B。将B推入A意味着将B中的所有元素插入到数组A中。这些元素将被添加到A的末尾。但是要实现这一点,我们必须检查一件事情,即A中剩余的空位(即A的最大大小减去A中现有元素的数量)是否与B中的元素数量相同或更大。否则,我们无法将它们推入A中。让我们看一下算法以及C++实现代码。

Algorithm

  • take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input

  • 如果 A 有足够的空间来容纳整个 B,则

  • for each element e in B, do

  • append e to array A

  • 结束循环

  • end if

  • return array A and new size n

Example

#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;
}

void pushArrayToAnother( int A[], int &n, int B[], int m ) {
   if( (Z - n) >= m ){
      for( int i = 0; i < m; i++ ) {
         insertAtEnd( A, n, B[i] );   
      }
   }
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   int B[ Z ] = {56, 84, 23, 12, 17, 19, 65, 32};
   int m = 8;
   
   cout << "First Array: ";
   displayArr( A, n );
   
   cout << "Second Array: ";
   displayArr( B, m );
   
   pushArrayToAnother( A, n, B, m );
   cout << "Array A after pushing B:" << endl;
   displayArr( A, n );
}
登录后复制

输出

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,
登录后复制
登录后复制
登录后复制

Using Dynamic Arrays or Vectors

相同的事情可以使用向量来完成。向量是C++ STL中存在的动态数组。如果我们考虑使用向量,我们就不需要关心插入元素时的可用空间。因为向量是动态的,它会在需要时自动添加新的插槽。算法与可用插槽检查相同。

Algorithm

  • take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input

  • for each element e in B, do

    • append e to array A

  • 结束循环

  • return array A and new size n

Example

#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;
}

void pushArrayToAnother( vector<int> &A, vector<int> B ){
   for( int i = 0; i < B.size() ; i++ ) {
      A.push_back( B[i] );
   }
}

int main(){
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};

   vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32};

   cout << "First Array: ";
   displayArr( A );

   cout << "Second Array: ";
   displayArr( B );

   pushArrayToAnother( A, B );
   cout << "Array A after pushing B:" << endl;
   displayArr( A );
}
登录后复制

输出

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,
登录后复制
登录后复制
登录后复制

在向量中使用insert()函数

之前的方法是一个手动的过程。然而,我们可以使用vector STL中的insert()函数来实现相同的功能。insert()函数接受一个位置指针(使用迭代器)和一个迭代器,从一个容器对象中复制元素并将其从位置索引插入到另一个容器对象中。让我们看一下C++的实现以获得清晰的视图。

Example

#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;
} 

void pushArrayToAnother( vector<int> &A, vector<int> B ) { 
   A.insert( A.end(), B.begin(), B.end() ); 
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   
   vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32};
   
   cout << "First Array: ";
   displayArr( A );
   
   cout << "Second Array: ";
   displayArr( B );
   
   pushArrayToAnother( A, B );
   cout << "Array A after pushing B:" << endl;
   displayArr( A );
}
登录后复制

输出

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,
登录后复制
登录后复制
登录后复制

Conclusion

在本文中,我们看到了几种不同的方法来将一个数组元素插入或推送到另一个数组的末尾。在第一个示例中,我们使用简单的C++数组,需要特别注意静态数组中可用空间的情况。在接下来的两种方法中,我们不需要关心这一点,因为我们使用的是动态的向量,它会在需要时自动分配空间。

以上是C++程序将一个数组推入另一个数组中的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板