#include<iostream> #include <vector> using namespace std; void main() { int beg,len;//子串开始位置,长度 vector<char> cvec;//声明容器 char temp; while (cin.get(temp)) cvec.push_back(temp);//添加元素 cout<<"Please input the starting position and the length of substring/n"; cin.clear();//恢复流状态 cin>>beg>>len; if((beg+len)>cvec.size())//判断求子串的输入是否有误 { cout<<"Wrong Input!"<<endl; return ; } for (vector<char>::iterator iter=cvec.begin()+beg-1;iter!=cvec.begin()+beg+len-1;iter++)//输出子串 cout<<*iter; cout<<endl; } //*********Andy于2010/7/7******************************* 看到有个同学求子串的程序写的有点乱,就自己动手写了一个如上。用STL实现,相当简洁