理解概念并举例说明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++实现代码。
algorithmtake 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 50using 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中存在的动态数组。如果我们考虑使用向量,我们就不需要关心插入元素时的可用空间。因为向量是动态的,它会在需要时自动添加新的插槽。算法与可用插槽检查相同。
algorithmtake 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 50using 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 50using 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++程序将一个数组推入另一个数组中的详细内容。
