您的位置:首页 > 其它

拆分string 用空格 逗号

2014-04-15 11:54 162 查看

stringthathavebothcommaandspace

structtokens:std::ctype<char>{
tokens():std::ctype<char>(get_table()){}staticstd::ctype_base::maskconst*get_table(){typedefstd::ctype<char>cctype;staticconstcctype::mask*const_rc=cctype::classic_table();staticcctype::maskrc[cctype::table_size];
std::memcpy(rc,const_rc,cctype::table_size*sizeof(cctype::mask));

rc[',']=std::ctype_base::space;
rc['']=std::ctype_base::space;return&rc[0];}};

std::strings="rightway,wrongway,correctway";
std::stringstreamss(s);
ss.imbue(std::locale(std::locale(),newtokens()));
std::istream_iterator<std::string>begin(ss);
std::istream_iterator<std::string>end;
std::vector<std::string>vstrings(begin,end);
std::copy(vstrings.begin(),vstrings.end(),std::ostream_iterator<std::string>(std::cout,"\n"));

——————————————————————————————————————————————————————————————————————————

只有空格:
trings="Whatistherightwaytosplitastringintoavectorofstrings";
std::stringstreamss(s);
std::istream_iterator<std::string>begin(ss);
std::istream_iterator<std::string>end;
std::vector<std::string>vstrings(begin,end);
std::copy(vstrings.begin(),vstrings.end(),std::ostream_iterator<std::string>(std::cout,"\n"));


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: