• 第8章 list双向链表容器


    /*
    
    第8章 list双向链表容器
       8.1 list技术原理
       8.2 list应用基础
       8.3 本章小结
    
    */
    
    
    
    //第8章 list双向链表容器
    
    //   8.1 list技术原理 -----------------------------------------------------------------------------------------
    
    //   8.2 list应用基础 -----------------------------------------------------------------------------------------
    
    
    // 125
    #include <list>
    #include <iostream>
    struct student
    {
      char *name;
      int age;
      char *city;
      char *tel;
    };
    int main(void)
    {
        student s[] = {
            {"符符", 18, "北京市", "67111111"},
            {"介介", 30, "上海市", "33939393"},
            {"贝贝", 23, "深圳市", "88883333"}
        };
      using namespace std;
      list < student > l;
      l.push_back(s[0]);
      l.push_back(s[1]);
      l.push_back(s[2]);
      list < student > ::iterator i, iend;
      iend = l.end();
      cout << "姓名    年龄    城市    电话" << endl;
      cout << "----------------------------------" << endl;
      for(i = l.begin(); i != iend; i++)
      {
        cout << (*i).name << "    ";
        cout << (*i).age << "    ";
        cout << (*i).city << "    ";
        cout << (*i).tel << "    " << endl;
      }
      cout << "----------------------------------" << endl << endl;
      return 0;
    }
    
    
    
    //126, push_front, insert
    #include <list>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      list < int > l;
      l.push_back(6);
      l.push_back(8);
      l.push_back(9);
      //插入链表元素
      list < int > ::iterator i, iend;
      i = l.begin(); // l.begin()+1,这样不可以
      i++; // 自增,找下一个节点
      l.insert(i, 7);
      l.push_front(5);
      //打印链表元素
      iend = l.end();
      for(i = l.begin(); i != iend; i++)
        cout <<  *i << ' ';
      return 0;
    }
    
    
    // 127, pop, erase
    #include <list>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      list < int > l;
      l.push_back(5);
      l.push_back(6);
      l.push_back(7);
      l.push_back(8);
      l.push_back(9);
      l.push_back(9);
      l.push_back(9);
      l.push_back(10);
      //删除元素,剩下7、8
      list < int > ::iterator i, iend;
      i = l.begin();
      i++;
      l.erase(i); // 6
      l.pop_back(); // 10
      l.pop_front(); // 5
      l.remove(9); // 9...
      //打印
      iend = l.end();
      for(i = l.begin(); i != iend; i++)
        cout <<  *i << ' ';
      cout << endl;
      return 0;
    }
    
    
    
    // splice
    // www.cplusplus.com/C++ Library Reference/stl/list/splice.html
    
    // splicing lists
    #include <iostream>
    #include <list>
    using namespace std;
    
    int main ()
    {
      list<int> mylist1, mylist2;
      list<int>::iterator it;
    
      // set some initial values:
      for (int i=1; i<=4; i++)
         mylist1.push_back(i);      // mylist1: 1 2 3 4
    
      for (int i=1; i<=3; i++)
         mylist2.push_back(i*10);   // mylist2: 10 20 30
    
      it = mylist1.begin();
      ++it;                         // points to 2
                                    // 将mylist2全部元素,插入到mylist1的位置it前。mylist2删除
      mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
                                    // mylist2 (empty)
                                    // "it" still points to 2 (the 5th element)
                                    // 将mylist1中it位置的元素,插入到mylist2的begin前,*it从mylist1中删除          
      mylist2.splice (mylist2.begin(),mylist1, it);
                                    // mylist1: 1 10 20 30 3 4
                                    // mylist2: 2
                                    // "it" is now invalid.
      it = mylist1.begin();
      advance(it,3);                // "it" points now to 30  ***** advance *****
      // 将mylist1(第二个参数)中,it->mylist1.end()中的元素(30 3 4),插入到mylist1的begin之前
      mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
                                    // mylist1: 30 3 4 1 10 20
    
      cout << "mylist1 contains:";
      for (it=mylist1.begin(); it!=mylist1.end(); it++)
        cout << " " << *it;
    
      cout << "
    mylist2 contains:";
      for (it=mylist2.begin(); it!=mylist2.end(); it++)
        cout << " " << *it;
      cout << endl;
    
      return 0;
    }
    
    
    
    // www.cplusplus.com/C++ Library Reference/stl/list/merge.html
    // list::merge
    #include <iostream>
    #include <list>
    using namespace std;
    
    // this compares equal two doubles if
    //  their interger equivalents are equal
    bool mycomparison (double first, double second)
    { return ( int(first)<int(second) ); }
    
    int main ()
    {
      list<double> first, second;
    
      first.push_back (3.1);
      first.push_back (2.2);
      first.push_back (2.9);
    
      second.push_back (3.7);
      second.push_back (7.1);
      second.push_back (1.4);
    
      first.sort();
      second.sort();
    
      first.merge(second);
    
      second.push_back (2.1);
    
      first.merge(second,mycomparison);
    
      cout << "first contains:";
      for (list<double>::iterator it=first.begin(); it!=first.end(); ++it)
        cout << " " << *it;
      cout << endl;
    
      return 0;
    }
    
    
    // 129 splice, merge
    #include <list>
    #include <iostream>
    using namespace std;
    void print(list < int >  &l);
    int main(void)
    {
      list < int > l;
      for(int j = 1; j <= 10; j++)
        l.push_back(j);
      //splice()函数
      list < int > carry;           // l中的第一个元素,插入carry,本身从l中删除
      carry.splice(carry.begin(), l, l.begin()); //carry含元素1
      //打印carry
      cout << "carry的链表元素为: ";
      print(carry);
      cout << endl;
      //打印l
      cout << "l的链表元素为: ";
      print(l);
      cout << endl;
      //merge()函数用法
      list < int > x;
      x.push_back(30);
      x.push_back(31);
      x.push_back(32);
      l.merge(x); // x中元素必须有序。将x中所有元素,归并到l中。然后x中元素全部被删除
      //打印x
      cout << "x的链表元素为: 空";
      print(x);
      cout << endl;
      //打印l
      cout << "l的链表元素为: ";
      print(l);
      cout << endl;
      return 0;
    }
    void print(list < int >  &l)
    {
      list < int > ::iterator i, iend;
      iend = l.end();
      for(i = l.begin(); i != iend; i++)
        cout <<  *i << ' ';
    }
    
    
    // 130 sort
    #include <list>
    #include <iostream>
    using namespace std;
    void print(list < int >  &l);
    int main(void)
    {
      list < int > l;
      for(int j = 18; j >= 0; j--)
        l.push_back(j);
      cout << "排序前: ";
      print(l);
      //调用list<int>::sort()函数排序
      l.sort(); // 默认为升序
      cout << "排序后: ";
      print(l);
      return 0;
    }
    void print(list < int >  &l)
    {
      list < int > ::iterator i, iend;
      iend = l.end();
      for(i = l.begin(); i != iend; i++)
        cout <<  *i << ' ';
      cout << endl;
    }
    
    
    // unique
    #include <list>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      list < int > l;
      l.push_back(6);
      l.push_back(8);
      l.push_back(6);
      l.push_back(6);
      l.push_back(6);
      l.push_back(9);
      l.push_back(13);
      l.push_back(6);
      l.unique(); // 去除全部*连续的*重复元素,仅剩下一个
      list < int > ::iterator i, iend;
      iend = l.end();
      for(i = l.begin(); i != iend; i++)
        cout <<  *i << ' ';
      cout << endl;
      return 0;
    }
    
    
    //   8.3 本章小结 -----------------------------------------------------------------------------------------

    TOP

  • 相关阅读:
    生产排程系统设计(二)设计思路
    一个ExtJs项目的Demo
    信息流与生产作业的同步与监控
    生产企业的IT人才储备困局
    生产排程系统设计(三)设计概要
    生产排程系统设计(一)设计愿景
    找工作时,你如何看待薪资问题
    sql 跨服务器 select ,update,insert
    Biztalk和Widows Wrokflow Fundation的区别和联系
    BizTalk 2010 虚拟环境分布式安装需要注意的问题
  • 原文地址:https://www.cnblogs.com/xin-le/p/4110712.html
Copyright © 2020-2023  润新知