STL list用法总结

2017-08-20 15:17:30

writer:pprp

list是一种线性复杂度的容器,很快

/*
name : usage of List
writer : pprp
declare : null
date ; 2017/8/20
*/
#include <bits/stdc++.h>

using namespace std; 

void print(list<int> & l)
{
      list<int>::iterator it;
      for(it = l.begin() ; it != l.end() ; it++)
      {
            cout << *it << " ";
      }
      cout << endl;
}

int main()
{
      list <int> l1;
      list <int> l2;
           
      rep(i,0,10)
      {
            l1.push_back(i);
      }
      
      print(l1);
      
      rep(i,2,20)
      {
            l2.push_back(i);
      }
      
      print(l2);
      
      list<int>::iterator it;
      
      it = l2.begin()++;
      
      l2.splice(l2.begin(),l1);//将l1中元素开始合并到l2上,起始点为l2.begin(),最后再删除掉l1
      
      print(l2);
      
      it = l2.begin();
      
      l1.splice(l1.begin(),l2, it); //将l2上it位置处的元素归并到l1上并从l2中删掉
      
      print(l1);
      
      l2.sort();
      
      print(l2);
      
      l2.unique(); //去重
      
      print(l2);
      
      rep(i,23,30)
      {
            l1.push_back(i);
      }
      
      print(l1);
      
      l2.merge(l1); //两个链表进行排序才能使用这个,归并以后的结果也是排序的结果
      
      print(l1);
      print(l2);
      return 0;
}

其他常用的函数:

push_back();
push_front();
erase();
pop_front();
pop_back();
remove();
clear();

 

posted @ 2017-08-20 15:22  pprp  阅读(213)  评论(0编辑  收藏  举报