zoukankan      html  css  js  c++  java
  • Problem G: STL——整理唱片(list的使用)

    #include<iostream>
    #include<list>
    #include<iterator>
    #include<algorithm>
    using namespace std;
    list<int> p;
    int ii, jj;
    bool op(int x)      /*这个很重要*/
    {
        return x <= ii;
    }
    int main()
    {
        int n;
        while(cin >> n)
        {
            for(int i = 0; i < n; i++)
            {
                int t;
                cin >> t;
                p.push_back(t);
            }
            int m;
            cin >> m;
            for(int i = 0; i < m; i++)
            {
                int t;
                cin >> t;
                switch (t)
                {
                    case 1:
                    {
                        cin >> ii >> jj;
                        list<int>::iterator it = find(p.begin(), p.end(), ii);
                        if(it != p.end())
                            p.insert(++it, jj);
                        break;
                    }
                    case 2:
                    {
                        cin >> ii;
                        p.remove_if(op);
     
     
    //                    list<int>::iterator it = find(p.begin(), p.end(), ii);
    //                    for(int i = ii; i >= 0; i--)
    //                        p.remove(i);
     
                        break;
                    }
                    case 3:
                    {
                        cin >> ii >> jj;
                        list<int>::iterator iit = find(p.begin(), p.end(), jj);
                        if(iit != p.end())      /*依据题目上的“注”*/
                            p.remove(ii);
                        list<int>::iterator it = find(p.begin(), p.end(), jj);
                        if(it != p.end())
                            p.insert(++it, ii);
                        break;
                    }
     
                }
            }
     
            cout << p.front();
            p.pop_front();
            while(!p.empty())
            {
                cout << " " << p.front();
                p.pop_front();
            }
            cout << endl;
        }
        return 0;
    }

    这里用到了remove_if(op), 不得不说,这个很好用,意思是list中满足op这个条件的元素将会被全部移除

  • 相关阅读:
    前端工程化浅学
    jQuery学习
    黄金票据和白银票据获取域控权限
    [转]0day零距离
    [转]走近0day
    [转]人生如解 -- 关于破解组织的乱弹
    [转]WAREZ无形帝国
    [转]BSD系统正在死亡?一些安全研究人员这样认为
    Solaris:你好奇的十件事
    Windows和Office激活汇总
  • 原文地址:https://www.cnblogs.com/KeepZ/p/11143772.html
Copyright © 2011-2022 走看看