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,则
立即学习“C++免费学习笔记(深入)”;
for each element e in B, do
append e to array A
结束循环
end if
return array A and new size n
Example
#include # define Z 50using namespace std;void displayArr(int arr[], int n){ for( int i = 0; i = m ){ for( int i = 0; i输出
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 #include # define Z 50using namespace std;void displayArr( vector v ){ for( int i = 0; i &A, vector B ){ for( int i = 0; i A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; vector B = {56, 84, 23, 12, 17, 19, 65, 32}; cout输出
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 #include # define Z 50using namespace std;void displayArr( vector v ){ for( int i = 0; i &A, vector B ) { A.insert( A.end(), B.begin(), B.end() ); }int main() { vector A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; vector B = {56, 84, 23, 12, 17, 19, 65, 32}; cout输出
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++程序将一个数组推入另一个数组中的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2583893.html