• C++总结笔记(九)常见容器List


    #include<iostream>
    #include<list>
    #include<numeric>
    #include <algorithm>
    using namespace std;

    void showList()
    {
     list<int> iList;
     iList.push_front(1);
     iList.push_front(2);
     iList.push_back(4);
     iList.push_back(3);
     
     list<int>::iterator i;

     cout<<"Display from the front:"<<endl;
     for(i = iList.begin();i != iList.end();i++)
     {
      cout<<*i<<" "<<endl;
     }

     list<int>::reverse_iterator j;
     cout<<"Display from the front:"<<endl;
     for(j = iList.rbegin();j != iList.rend();j++)
     {
      cout<<*j<<" "<<endl;
     }
    }

    void addList()
    {
     list<int> iList;
     iList.push_front(1);
     iList.push_front(2);
     iList.push_back(4);
     iList.push_back(3);

     int result = accumulate(iList.begin(),iList.end(),0);
     cout<<"The result is:"<<result<<endl;
    }

    void put_list(list<int>lists,char* name)
    {
     list<int>::iterator i;

     cout<<"The "<<name<<"will display:";

     for(i=lists.begin();i!=lists.end();i++)
     {
      cout<<*i<<" ";
     }
     cout<<endl;
    }

    void testList()
    {
     list<int> list1;
     list<int> list2(5,6);
     list<int> list3(list2.begin(),--list2.end());

     put_list(list1,"list1");
     put_list(list2,"list2");
     put_list(list3,"list3");
    }

    void insert()
    {
     list<int> list1(2,3);
     list1.insert(++list1.begin(),1,4);
     put_list(list1,"list1");
    }

    void delList()
    {
     list<int> list1;
     list1.push_front(1);
     list1.push_front(2);
     list1.push_front(3);
     list1.push_front(4);
     list1.push_front(5);
     list1.push_front(6);
     list1.push_front(7);
     list1.push_front(8);
     put_list(list1,"list1");

     list1.pop_front();
     list1.pop_back();
     put_list(list1,"list1");

     list1.erase(list1.begin(),--list1.end());
     put_list(list1,"list1");
    }

    int main()
    {
     //showList();
     //addList();
     //testList();
     //insert();
     delList();
     return 0;
    }

  • 相关阅读:
    .NE 学习概要
    (转)工作之路---记录LZ如何在两年半的时间内升为PM
    XP下Winform背景透明问题
    CSE(Corrupted State Exceptions) 严重异常处理办法
    (转)C#模拟键盘鼠标事件
    (转).net项目技术选型总结
    (转)MSMQ续
    (转)MSMQ(消息队列)
    (转)TCP三次握手
    Socket编程初探
  • 原文地址:https://www.cnblogs.com/huochangjun/p/3114620.html
Copyright © 2020-2023  润新知