• C++11 list::splice()函数的使用


    1、list::splice()原型

    list1.splice (iterator position, list2)
                    or 
    list1.splice (iterator position, list2, iterator i)
                    or 
    list1.splice (iterator position, list2, iterator first, iterator last)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、实现的功能

    2.1、 list1.splice (iterator position, list2)

    把list2中的内容剪切(不是复制)并从list1的position位置开始插入。

    案例1:从list1的头部位置开始全部插入

    int main()
    {
        std::list<int> pre{1,2,3,4,5,6,7,8,9,10};
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
        list<int> re{20,30,40};
        int datasize=5;
        re.splice(re.begin(),pre);
        for(auto j:re)
        {
            cout<<j<<" ";
        }
        cout<<endl;
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
        return 0;
    }
    
    打印结果:
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 20 30 40 
    
    
    • 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
    • 27
    • 28

    案例2:从list1的第二个位置开始全部插入

    int main()
    {
        std::list<int> pre{1,2,3,4,5,6,7,8,9,10};
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
        list<int> re{20,30,40};
        int datasize=5;
        re.splice(++re.begin(),pre);
        for(auto j:re)
        {
            cout<<j<<" ";
        }
        cout<<endl;
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
        return 0;
    }
    打印结果:
    1 2 3 4 5 6 7 8 9 10 
    20 1 2 3 4 5 6 7 8 9 10 30 40 
    
    
    • 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
    • 27

    案例3:从list1的尾部开始全部插入

    int main()
    {
        std::list<int> pre{1,2,3,4,5,6,7,8,9,10};
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
    
        list<int> re{20,30,40};
        int datasize=5;
        re.splice(re.end(),pre);
        for(auto j:re)
        {
            cout<<j<<" ";
        }
        cout<<endl;
    
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
        return 0;
    }
    打印结果:
    1 2 3 4 5 6 7 8 9 10 
    20 30 40 1 2 3 4 5 6 7 8 9 10 
    
    
    • 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
    • 27
    • 28
    • 29

    2.2、list1.splice (iterator position, list2, iterator i)

    把list2中从i位置的单个数据剪切(不是复制)并插入到list1的position位置
    案例1:把list2的首元素插入到list1的首位

    int main()
    {
        std::list<int> pre{1,2,3,4,5,6,7,8,9,10};
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
    
        list<int> re{20,30,40};
        int datasize=5;
        re.splice(re.begin(),pre,pre.begin());
        for(auto j:re)
        {
            cout<<j<<" ";
        }
        cout<<endl;
    
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
        return 0;
    }
    
    打印结果:
    1 2 3 4 5 6 7 8 9 10 
    1 20 30 40 
    2 3 4 5 6 7 8 9 10 
    
    • 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
    • 27
    • 28
    • 29
    • 30

    2.3、list1.splice (iterator position, list2, iterator first, iterator last)

    把list2中从first开始到last结束 (不包括last位置),这段区间内的数据剪切并从list1的position的位置开始插入。

    案例1:把list2的前两个元素插入到list1的开始

    int main()
    {
        std::list<int> pre{1,2,3,4,5,6,7,8,9,10};
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
    
        list<int> re{20,30,40};
        int datasize=5;
        re.splice(re.begin(),pre,pre.begin(),++(++pre.begin()));
        for(auto j:re)
        {
            cout<<j<<" ";
        }
        cout<<endl;
    
        for(auto i:pre)
        {
            cout<<i<<" ";
        }
        cout<<std::endl;
        return 0;
    }
    打印结果:
    1 2 3 4 5 6 7 8 9 10 
    1 2 20 30 40 
    3 4 5 6 7 8 9 10 
    
    • 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
    • 27
    • 28
    • 29
  • 相关阅读:
    AimBetter洞察您的数据库,DPM 和 APM 解决方案
    hadoop组成
    101. 对称二叉树
    Kafka总结文档
    Django学习一:创建Django框架,介绍Django的项目结构和开发逻辑。创建应用,编写主包和应用中的helloworld
    SpringBoot使用JSP
    git代码管理基础操作
    pytorch图像变换
    数据库DQL数据查询语言
    PS-HDR图像编辑与应用
  • 原文地址:https://blog.csdn.net/u011573853/article/details/126296574