news 2026/8/6 2:15:46

C++两个数组连接类似python的list相加

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++两个数组连接类似python的list相加

C++中实现Python列表拼接操作

📋 完整代码示例

#include<iostream>#include<vector>#include<algorithm>#include<iterator>// 方法1:基础实现std::vector<int>concatenate_vectors(conststd::vector<int>&v1,conststd::vector<int>&v2){std::vector<int>result=v1;result.insert(result.end(),v2.begin(),v2.end());returnresult;}// 方法2:性能优化版本std::vector<int>concatenate_vectors_optimized(conststd::vector<int>&v1,conststd::vector<int>&v2){std::vector<int>result;result.reserve(v1.size()+v2.size());result.insert(result.end(),v1.begin(),v1.end());result.insert(result.end(),v2.begin(),v2.end());returnresult;}// 方法3:使用std::copystd::vector<int>concatenate_vectors_copy(conststd::vector<int>&v1,conststd::vector<int>&v2){std::vector<int>result;std::copy(v1.begin(),v1.end(),std::back_inserter(result));std::copy(v2.begin(),v2.end(),std::back_inserter(result));returnresult;}// 方法4:移动语义版本(C++11+)std::vector<int>concatenate_vectors_move(std::vector<int>&&v1,std::vector<int>&&v2){std::vector<int>result=std::move(v1);result.insert(result.end(),std::make_move_iterator(v2.begin()),std::make_move_iterator(v2.end()));returnresult;}// 使用示例intmain(){std::vector<int>vec1={1,2,3};std::vector<int>vec2={4,5,6};// 基础使用std::vector<int>combined1=concatenate_vectors(vec1,vec2);// 优化版本std::vector<int>combined2=concatenate_vectors_optimized(vec1,vec2);// 移动语义版本std::vector<int>vec3={1,2,3};std::vector<int>vec4={4,5,6};std::vector<int>combined3=concatenate_vectors_move(std::move(vec3),std::move(vec4));// 输出结果for(intnum:combined1){std::cout<<num<<" ";}std::cout<<std::endl;return0;}

🔧 重载运算符版本(仿Python语法)

如果您希望在C++中获得类似Python的+运算符语法,可以重载运算符:

#include<vector>// 重载+运算符,返回新向量template<typenameT>std::vector<T>operator+(conststd::vector<T>&lhs,conststd::vector<T>&rhs){std::vector<T>result;result.reserve(lhs.size()+rhs.size());result.insert(result.end(),lhs.begin(),lhs.end());result.insert(result.end(),rhs.begin(),rhs.end());returnresult;}// 重载+=运算符,修改左侧向量template<typenameT>std::vector<T>&operator+=(std::vector<T>&lhs,conststd::vector<T>&rhs){lhs.reserve(lhs.size()+rhs.size());lhs.insert(lhs.end(),rhs.begin(),rhs.end());returnlhs;}// 使用示例intmain(){std::vector<int>vec1={1,2,3};std::vector<int>vec2={4,5,6};// 使用+运算符(仿Python语法)std::vector<int>result=vec1+vec2;// {1, 2, 3, 4, 5, 6}// 使用+=运算符vec1+=vec2;// vec1现在是{1, 2, 3, 4, 5, 6}return0;}

📊 性能对比分析

方法 时间复杂度 空间复杂度 适用场景
基础insert O(n+m) O(n+m) 通用场景
预分配reserve O(n+m) O(n+m) 大数据量,性能敏感
std::copy O(n+m) O(n+m) 函数式编程风格
移动语义 O(n+m) O(1)移动成本 确定不再使用原向量
运算符重载 O(n+m) O(n+m) 希望语法类似Python

💡 最佳实践建议

  1. 小规模数据:直接使用基础insert方法,代码最简洁
  2. 大规模数据:使用预分配reserve的优化版本
  3. 临时数据拼接:使用移动语义避免不必要的复制
  4. 追求Python风格:实现运算符重载,但需注意与STL惯例的一致性
  5. 泛型编程:使用模板使其适用于各种类型:
template<typenameT>std::vector<T>concat(conststd::vector<T>&v1,conststd::vector<T>&v2){std::vector<T>result;result.reserve(v1.size()+v2.size());result.insert(result.end(),v1.begin(),v1.end());result.insert(result.end(),v2.begin(),v2.end());returnresult;}// 支持任意数量的向量拼接template<typenameT,typename...Vectors>std::vector<T>concat_multiple(conststd::vector<T>&first,constVectors&...vectors){std::vector<T>result;size_t total_size=first.size()+(vectors.size()+...);result.reserve(total_size);(result.insert(result.end(),vectors.begin(),vectors.end()),...);returnresult;}

您的整理已经非常完善,这些补充内容可以作为进一步的参考。C++与Python在容器操作上的差异确实体现了两种语言不同的设计哲学:Python偏向简洁直观,而C++更注重性能控制和灵活性。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/6 11:38:53

Java智能优化引擎避坑指南:从业务痛点到技术实战心法

"为什么我的排班系统总是被投诉&#xff1f;配送路线规划怎么老是超预算&#xff1f;生产计划排程每次都要手动调整到半夜&#xff1f;" 【免费下载链接】awesome-java A curated list of awesome frameworks, libraries and software for the Java programming lang…

作者头像 李华
网站建设 2026/8/5 8:42:20

Vue3 Excel Editor:终极Excel风格数据编辑解决方案

还在为数据表格编辑功能而烦恼吗&#xff1f;想要在Vue3项目中实现类似Excel的直观操作体验&#xff1f;Vue3 Excel Editor正是你需要的完美工具&#xff01;这款专为Vue3设计的插件&#xff0c;能够以Excel风格高效显示和编辑对象数组数据&#xff0c;让数据管理工作变得前所未…

作者头像 李华
网站建设 2026/8/5 6:55:14

如何快速掌握大麦抢票助手:完整使用指南与实用技巧

还在为心仪演出门票秒空而苦恼吗&#xff1f;大麦抢票助手正是您需要的得力助手。这款基于Python和Selenium开发的自动化工具&#xff0c;能够智能模拟真实用户操作&#xff0c;显著提升热门演出门票的获取概率。本文将为您提供从环境搭建到成功抢票的全流程指导。 【免费下载链…

作者头像 李华
网站建设 2026/8/5 12:51:10

基于Spring Boot的智慧校园管理系统设计与实现

背景及意义 进入二十一世纪&#xff0c;互联网的飞速发展彻底重塑了人们的生活与信息传播模式&#xff0c;打破了传统地域限制带来的思想鸿沟&#xff0c;取代了骑马、信鸽等低效且不稳定的信息传递方式&#xff0c;推动各行业迈向信息化、数字化管理新阶段。当前&#xff0c;校…

作者头像 李华
网站建设 2026/8/5 11:37:57

Chinese-CLIP深度性能评估:三大基准数据集全面解析

项目技术全景 【免费下载链接】Chinese-CLIP 针对中文场景下设计和构建的CLIP模型变体&#xff0c;它能够完成跨视觉与文本模态的中文信息检索&#xff0c;并能够生成有效的多模态表示。这样的工具主要用于提升人工智能系统对于不同模态&#xff08;如图像和文本&#xff09;数…

作者头像 李华
网站建设 2026/8/5 12:39:01

import_3dm完整教程:轻松实现Rhino到Blender的无缝数据迁移

import_3dm完整教程&#xff1a;轻松实现Rhino到Blender的无缝数据迁移 【免费下载链接】import_3dm Blender importer script for Rhinoceros 3D files 项目地址: https://gitcode.com/gh_mirrors/im/import_3dm 还在为Rhino模型无法直接在Blender中使用而烦恼吗&#…

作者头像 李华