zoukankan      html  css  js  c++  java
  • 【STL】-list的用法

    初始化

    #include <list>

    list<char> clist;

    算法:

    clist.push_back(c);

    clist.remove('d');

    代码:

     1 #include <list>
     2 #include <iostream>
     3 #include <iterator>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 int main() 
     8 {
     9     list<char> clist;
    10 
    11     //can't put iter initialization here
    12     //because clist is empty now, iter will be NULL
    13     //list<char>::iterator iter = clist.begin();    
    14     
    15     for(char c = 'a'; c < 'f'; ++c)
    16         clist.push_back(c);
    17 
    18     list<char>::iterator iter = clist.begin();    
    19 
    20     for(; iter != clist.end(); ++iter)
    21         cout << *iter << " | ";
    22 
    23     cout << endl;
    24 
    25     //高效率remove,利用list特点,只移动一个元素
    26     clist.remove('d');
    27     copy(clist.begin(), clist.end(), ostream_iterator<char>(cout, " "));
    28 
    29     cout << endl;
    30 
    31     //低效率remove, 算法,需要移动很多元素
    32     clist.erase(remove(clist.begin(), clist.end(), 'c'), clist.end());
    33     copy(clist.begin(), clist.end(), ostream_iterator<char>(cout, " "));    
    34 
    35     return 0;
    36 }

    输出:

    $ ./a.exe
    a | b | c | d | e |
    a b c e
    a b e
  • 相关阅读:
    「USACO 2020.12 Platinum」Sleeping Cows
    拉格朗日反演 (Lagrange Inversion)
    「ROI 2016 Day1」人烟之山
    「ROI 2016 Day2」二指禅
    ZJOI2016 大森林
    CF1119H Triple
    [ZJOI2016]线段树
    CF1237F
    NOI2018 情报中心
    CF1270I Xor on Figures
  • 原文地址:https://www.cnblogs.com/dracohan/p/3896915.html
Copyright © 2011-2022 走看看