zoukankan      html  css  js  c++  java
  • 链表list容器中通过splice合并链表与merge的不同,及需要注意的问题

    #include "stdafx.h"

    #include <iostream>

    #include <list>

    #include <algorithm>

    using namespace std;

    int_tmain(int argc, _TCHAR* argv[])

    {

        list<int> c1,c2,c3,c4;

        c1.push_back(3);

        c1.push_back(6);

        c2.push_back(2);

        c2.push_back(4);

        c3.push_back(5);

        c3.push_back(1);

        c4.push_back(40);

        c4.push_back(41);

        cout<<"c1="<<endl;

        copy(c1.begin(),c1.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        cout<<"c2="<<endl;

        copy(c2.begin(),c2.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        c2.splice(c2.begin(),c1);//将链表c1插入到链表c2的链头:第一种使用方式

        cout<<"After splice c1 and c2>: c2=: "<<endl;

        copy(c2.begin(),c2.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        cout<<"After splice c1 and c2: c1="<<endl;

        copy(c1.begin(),c1.end(),ostream_iterator<int>(cout,""));

        cout<<"可见splice合并后c1中没有内容了"<<endl;

        cout<<"c3="<<endl;

        copy(c3.begin(),c3.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        c2.splice(c2.begin(),c3,c3.begin());//将c3链表的头元素插入c2链表的头部:第二种使用方式

        cout<<"After splice c2 and c3: c2=: "<<endl;

        copy(c2.begin(),c2.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        cout<<"After splice c3="<<endl;

        copy(c3.begin(),c3.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        cout<<"可见splice后,c3的头元素不在了"<<endl;

        cout<<"c4="<<endl;

        copy(c4.begin(),c4.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        c2.splice(c2.begin(),c4,c4.begin(),c4.end());//将链表c4从开始到结束都合并到c2开始的位置:第三种使用方式

        cout<<"After splice c2 and c4: c2=: "<<endl;

        copy(c2.begin(),c2.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        cout<<"After splice c4="<<endl;

        copy(c4.begin(),c4.end(),ostream_iterator<int>(cout,""));

        cout<<endl;

        cout<<"可见splice合并后c4中没有内容了"<<endl;

       

        return 0;

    }

    执行结果:

    可见:splice与merge最大的不同时,不用排序,也不要求原始链表有序。相同点是,被合并的链表或元素将消失。

  • 相关阅读:
    centos安装vim
    thrift学习之二----学习资料积累
    thrift学习之一-------介绍
    组合模式
    一致性哈希算法(consistent hashing)
    php配置php-fpm启动参数及配置详解
    error while loading shared libraries的解決方法
    数据结构之二叉树
    768、最多能完成排序的块(贪心算法)
    VS code 配置C++编译环境
  • 原文地址:https://www.cnblogs.com/pangblog/p/3339498.html
Copyright © 2011-2022 走看看