zoukankan      html  css  js  c++  java
  • STL之顺序容器

    一、顺序容量STL list类(双向链表)

      list是一个模板类,可以在list头部、尾部、中间任意部位很方便地插入元素,这就区别于STL的其他模板类;

      vector向量只能在尾部插入数据;

      deque可以在头部和尾部拆入;

      标准模板类中实现了对list中元素的反转和排序方法;

     1 #include <iostream>
     2 #include <list>
     3 
     4 using namespace std;
     5 void PrintList(const list<int>& input)
     6 {
     7     std::cout << std::endl;
     8     std::cout << "{ ";
     9     //for(std::list<int>::iterator it=input.begin();it!=input.end();it++){
    10     for(std::list<int>::const_iterator it=input.begin();it!=input.end();it++){
    11         std::cout <<*it <<" ";
    12     }
    13     std::cout << "}" << std::endl;
    14 }
    15 
    16 int main()
    17 {
    18     std::list<int> a;
    19     std::list<int> b;
    20     std::list<int>::iterator iter;
    21 
    22     b.push_back(100);
    23     b.push_back(200);
    24     b.push_back(300);
    25     b.push_back(400);
    26     PrintList(b);
    27     c.push_front(1); 
    28     PrintList(b);
    29 
    30     a.push_front(4);   //头插
    31     a.push_front(3);
    32     a.push_front(2);
    33     a.push_front(1);
    34     a.push_back(5);   //尾插
    35 
    36     iter = a.begin();
    37     ++iter;
    38     a.insert(iter, 10); //通过迭代器位置的改变来插入数据;
    39     a.insert(a.end(), 4, 20);
    40     PrintList(a);
    41 
    42     iter = a.begin();
    43     ++iter;
    44     a.insert(iter, ++b.begin(), --b.end());
    45     PrintList(a);
    46 
    47     std::list<int> c;
    48     c.push_back(4);
    49     c.push_front(3);
    50 
    51     list<int>::iterator it = c.insert(c.begin(), 2);
    52     c.push_front(1);
    53     c.push_front(0);
    54     PrintList(c);
    55     //c.erase(it);
    56     //PrintList(c);
    57 
    58     //”左包右不包“
    59     c.erase(c.begin(), it);
    60     PrintList(c);
    61 
    62     std::list<int> d;
    63     d.push_front(34);
    64     d.push_front(39);
    65     d.push_front(2);
    66     d.push_front(199);
    67     d.push_front(90);
    68     PrintList(d);
    69 
    70     d.reverse();    //反转list
    71     PrintList(d);
    72 
    73     d.sort();       //给list排序
    74     PrintList(d);
    75 
    76     return 0;
    77 }

      内容待补充..... 

  • 相关阅读:
    jquery处理鼠标左中右键事件
    bootstrap弹出框
    移动端去掉a标签点击时出现的背景
    sessionStorage
    页面滑动到最下面,执行代码
    判断页面时向上滚动还是向下滚动
    sql 时间查询 /sql中判断更新或者插入/查询一年所有双休日
    求取最大值
    Repeater 获取数据值
    加载完毕后执行计算
  • 原文地址:https://www.cnblogs.com/chris-cp/p/4507175.html
Copyright © 2011-2022 走看看