uva11988

来源:互联网 发布:网络玄幻小说家排名 编辑:程序博客网 时间:2024/05/24 07:20
#include <iostream>#include <list>#include <stdio.h>#include <string.h>using namespace std;char c[100010];list<char> s;int main(){    while(~scanf("%s",c))    {        s.clear();        list<char>::iterator it=s.begin();        int len=strlen(c);        for(int i=0; i<len; i++)        {            if(c[i]=='[')                it=s.begin();            else if(c[i]==']')                it=s.end();            else                s.insert(it,c[i]);        }        for(list<char>::iterator i=s.begin(); i!=s.end(); i++)            printf("%c",(*i));        cout<<endl;    }    return 0;}

注意那个insert……的插入顺序!
我一开始写的是:

s.insert(it++,c[i]);//错误

参考以下代码

// inserting into a list#include <iostream>#include <list>#include <vector>int main (){  std::list<int> mylist;  std::list<int>::iterator it;  // set some initial values:  for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5  it = mylist.begin();  ++it;       // it points now to number 2           ^  mylist.insert (it,10);                        // 1 10 2 3 4 5  // "it" still points to number 2                      ^  mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5  --it;       // it points now to the second 20            ^  std::vector<int> myvector (2,30);  mylist.insert (it,myvector.begin(),myvector.end());                                                // 1 10 20 30 30 20 2 3 4 5                                                //               ^  std::cout << "mylist contains:";  for (it=mylist.begin(); it!=mylist.end(); ++it)    std::cout << ' ' << *it;  std::cout << '\n';  return 0;}