想用一个循环遍历string,大家看看这是为什么。。
string s=“a a a a”;
auto beg=s.begin();
while(beg!=s.end()){
stemp.clear();
while(beg!=s.end() && *beg==' '){//为什么执行完这两个循环的时候已经到end了??????
++beg;
}
while(beg!=s.end() && *beg!=' '){
stemp+=*beg;
++beg;
}
if(beg==s.end()){cout<<"x";}//这里是为了检验才写的,结果中只输出了一次x,也就是循环只执行了一次就到尾了,不应该啊
std::string s= "a a a a";
std::string stemp;
auto beg = s.begin();
while(beg != s.end()){
//stemp.clear();
while(beg !=s.end() && *beg==' '){//为什么执行完这两个循环的时候已经到end了??????
++beg;
}
while(beg!=s.end() && *beg!=' '){
stemp+=*beg;
++beg;
}
}
std::cout<<stemp<<std::endl;
std::string s = "a a a a";
std::string stemp;
std::remove_copy(std::begin(s), std::end(s), std::back_inserter(stemp), ' ');
std::cout<<stemp<<std::endl;
s.erase((std::remove(std::begin(s), std::end(s), ' ')), std::end(s));
std::cout<<s<<std::endl;