zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    (一)linux 系统命令与文件
    python(1)- 初识python
    四、计算机硬件知识整理
    三、计算机硬件历史
    二、网络基础之网络协议
    jmeter while循环使用
    gitlab分支管理
    gitlab基本操作
    JAVA多种向influxDB中插入数据方式
    JAVA类变量和实例变量
  • 原文地址:https://www.cnblogs.com/huochangjun/p/3114620.html
Copyright © 2011-2022 走看看