码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++20 std::swap_ranges 用法小结


    swap_ranges() 用来交换两个序列。这个算法需要 3 个正向迭代器作为参数:前两个参数分别是第一个序列的开始和结束迭代器,第三个参数是第二个序列的开始迭代器。

    Possible implementation

    这个算法会返回一个迭代器,它指向第二个序列的最后一个被交换元素的下一个位置。

    template<class ForwardIt1, class ForwardIt2>
    constexpr ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
    {
        while (first1 != last1) {
            std::iter_swap(first1++, first2++);
        }
        return first2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    示例代码

    #include 
    #include 
    #include 
    #include 
    
    auto print = [](auto comment, auto const& seq) {
    	std::cout << comment;
    	for (const auto& e : seq) { std::cout << e << ' '; }
    	std::cout << '\n';
    };
    
    int main()
    {
    	std::vector<char> vct = { 'a', 'b', 'c', 'd', 'e' };
    	std::list<char> lst = { '1', '2', '3', '4', '5', '6' };
    
    	print("Before swap_ranges:\n" "v: ", vct);
    	print("l: ", lst);
    
    	std::list<char>::iterator itr = std::swap_ranges(vct.begin(), vct.begin() + 3, lst.begin());
    	int val = std::distance(lst.begin(), itr);
    	std::cout << "distance=" << val << "\n";
    
    	print("After swap_ranges:\n" "v: ", vct);
    	print("l: ", lst);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    输出结果

    Before swap_ranges:
    v: a b c d e
    l: 1 2 3 4 5 6
    distance=3
    After swap_ranges:
    v: 1 2 3 d e
    l: a b c 4 5 6
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    官方文档:

    https://en.cppreference.com/w/cpp/algorithm/swap_ranges

  • 相关阅读:
    状态管理Vuex
    BUUCTF 九连环 1
    hive笔记(二):数据类型-基本数据类型/集合数据类型/类型转换
    【NOI2020】制作菜品(构造,结论,背包DP,bitset优化)
    继承的使用以及细节问题
    ava官网下载离线api文档地址
    【论文研读】-Defining the Ethereum Virtual Machine for Interactive Theorem Provers
    余度设计概述
    通关GO语言21 网络编程:Go 语言如何玩转 RESTful API 服务?
    Java调用py或者exe文件实现word转PDF
  • 原文地址:https://blog.csdn.net/hellokandy/article/details/126778058
  • 最新文章
  • 【FHE】我们如何实现同态加密推理(八):SiLU 的密文化——两条路径,和一个 8 字节的开关
    SQLcl 中 c/old/new 替换变量为何失效?先定位再替换
    不让模型写作文,直接从它脑子里读答案:Jev 决策在 .NET 的两条路线
    Halo插件:支持GitHub的图床附件策略
    电视投屏界的“一股清流”!SimpleTVCast 6.0.0-beta 抢先体验:无需TV端App,界面大换血!✨
    华三VXLAN集中式网关实战指南:Spine多活网关 + 双活接入(M-LAG)
    软件更好写之后口碑会更重要
    大 O、大 Omega、大 Theta 的区别,O(n) 复杂度是什么意思
    Java标签跳转:一个鲜为人知但却很实用的小技巧
    多智能体系统论文速读
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号