这是怎么回事?
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[])
{
string input;
cout<<"Input your string: ";
cin>>input;
string::size_type
pos=0,prev_pos=0;
vector<string> svec;
while((pos=input.find_first_of(' ',pos))!=string::npos)
{
string::size_type end_pos=pos-prev_pos;
svec.push_back(input.substr(prev_pos,end_pos));
prev_pos=++pos;
}
svec.push_back(input.substr(prev_pos,pos-prev_pos));
vector<string>::iterator it=svec.begin(),
end_it=svec.end();
while(it!=end_it)
{cout<< *it<<endl;++it;}
getch();
return 0;
}
代码无法正确分割字符串
如输入:I am a student.
则只分割出一个I就自动跳出了,但是将while((pos=input.find_first_of(' ',pos))!=string::npos)改为while((pos=input.find_first_of('#',pos))!=string::npos)后
输入:I#am#a#student.
则可以正确分割出I,am,a,student四个string,请问这是为什么呀?
问题点数:20、回复次数:3Top
1 楼hcpp(简单的才是美的)回复于 2002-04-02 08:45:44 得分 10
假若你看看
cin>>input; 此时的内容就会知道答案了!//空格为默认分界符.
Top
2 楼jyc_nj(老蔣)回复于 2002-04-02 09:20:05 得分 10
同意顶楼。
其实这是个练你调试的好机会;)Top
3 楼ehost(炙日)回复于 2002-04-02 12:23:38 得分 0
谢谢两位,尤其是hcpp。Top




