c++的类库太强大了!给力! #include<iostream> #include<cstdio> #include<string> #include<set> using namespace std; string cla="class",be="begin",end="end",sub="Sub",def="def",call="call",undef="undef"; string ch,t1,t2; set<string> duck; set<string>::iterator it;//迭代器 int main() { int t; int i,j,len,k; cin>>t; while(t--) { cin>>ch; while(ch.compare (end)!=0)//比较函数 { if(ch==be) {}; if(ch==cla) { cin>>t1; if(t1.at(3)==':') { len=t1.size (); t2=t1.substr (4,len-1);//复制 it=duck.find(t2); if(it==duck.end ()) { printf("oops!/n"); } else { /*printf("%s %s/n",ch.c_str() ,t1.c_str ());*/string 用printf输出 if(duck.count (t1)!=0) printf("oops!/n"); else { cout<<ch<<" "<<t1<<endl; duck.insert (t1); } } } else { if(duck.count (t1)==0) { cout<<ch<<" "<<t1<<endl; duck.insert (t1); } else printf("oops!/n"); } } if(ch==def) { cin>>t1; int point=t1.find ('.'); t2=t1.substr (0,point-1); if(duck.find (t2)==duck.end ()) printf("oops!/n"); else { if(duck.find (t1)==duck.end ()) { cout<<"def"<<" "<<t1<<endl; duck.insert (t1); } } } if(ch==call) { cin>>t1; } cin>>ch; } } return 0; }