1 .按字符串搜索和substr
# include iostream # includestringusingnamespacestd; int main () { string str='How are you?' ; string t=' '; 字符串结果; size_t size_pos=0; size_t size_prev_pos=0; while ((size_pos=str.find _ first _ of ) t,size _ pos ) )!=string :3360 NPOs ({ cout size _ pos endl; ST result=str.substr (size _ prev _ pos,size_pos - size_prev_pos ); size_prev_pos=size_pos 1; size_pos; cout stresult endl; }if(size_prev_pos!=str.size () ST result=str.substr (size _ prev _ pos,size_pos - size_prev_pos ); cout stresult endl; } return 0; ) 2、用字符串流的方式
# include iostream # includes stream # includestringintmain () STD :3360 stringstr=' how are you '; STD :3360 istringstream is (str ); STD :字符串结果; while (! is.eof () ) STD:getline(is,stresult,''); STD : coutstresultstd : endl; } return 0; }