0

0

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

WBOY

WBOY

发布时间:2023-09-04 13:37:06

|

1708人浏览过

|

来源于tutorialspoint

转载

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,则

    立即学习C++免费学习笔记(深入)”;

  • for each element e in B, do

  • append e to array A

  • 结束循环

  • end if

  • return array A and new size n

    crmeb电商系统
    crmeb电商系统

    CRMEB 是基于Thinkphp5基础开发的以会员为中心的电商系统,开源版微信公众号商城和小程序商城数据同步,带积分、优惠券、秒杀、砍价、分销等功能,更是一套方便二次开发的商城框架(后台封装了独有快速创建表单功能,无需写表单页面、快速创建数据搜索和数据列表页、导出表格、系统权限配置控制每一个控制器方法、系统参数配置、数据字典、组合数据等)

    下载

Example

#include 
# 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 
#include 
# define Z 50

using namespace std;

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

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

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: ";
   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 
#include 
# define Z 50

using namespace std;

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

void pushArrayToAnother( vector &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: ";
   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++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

738

2023.08.22

append用法
append用法

append是一个常用的命令行工具,用于将一个文件的内容追加到另一个文件的末尾。想了解更多append用法相关内容,可以阅读本专题下面的文章。

343

2023.10.25

python中append的用法
python中append的用法

在Python中,append()是列表对象的一个方法,用于向列表末尾添加一个元素。想了解更多append的更多内容,可以阅读本专题下面的文章。

1070

2023.11.14

python中append的含义
python中append的含义

本专题整合了python中append的相关内容,阅读专题下面的文章了解更多详细内容。

174

2025.09.12

数据库Delete用法
数据库Delete用法

数据库Delete用法:1、删除单条记录;2、删除多条记录;3、删除所有记录;4、删除特定条件的记录。更多关于数据库Delete的内容,大家可以访问下面的文章。

269

2023.11.13

drop和delete的区别
drop和delete的区别

drop和delete的区别:1、功能与用途;2、操作对象;3、可逆性;4、空间释放;5、执行速度与效率;6、与其他命令的交互;7、影响的持久性;8、语法和执行;9、触发器与约束;10、事务处理。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

208

2023.12.29

点击input框没有光标怎么办
点击input框没有光标怎么办

点击input框没有光标的解决办法:1、确认输入框焦点;2、清除浏览器缓存;3、更新浏览器;4、使用JavaScript;5、检查硬件设备;6、检查输入框属性;7、调试JavaScript代码;8、检查页面其他元素;9、考虑浏览器兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

180

2023.11.24

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

402

2023.08.14

Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
微信小程序开发之API篇
微信小程序开发之API篇

共15课时 | 1.2万人学习

进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

c语言项目php解释器源码分析探索
c语言项目php解释器源码分析探索

共7课时 | 0.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号