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
  • 相关阅读:
    echo 变量不加引号出错
    linux以16进制方式查看文件
    批量删除符合条件的文件
    sed删除行
    linux用户环境变量
    脚本路径问题_dirname
    shell脚本返回字符串
    关于Unix时间戳
    grunt用来压缩前端脚本
    JAVA ThreadPoolExecutor(转)
  • 原文地址:https://www.cnblogs.com/dracohan/p/3896915.html
Copyright © 2011-2022 走看看