【摘要】 前言
C++ 作为老大哥竟然一直不支持 split ,在看程设模拟的时候发现了可以通过 getline 优雅的实现 split。
代码
vector<string> split(const string& s, char c) { //分隔文件名 vector<string> res; string tmp; stringstream ss(s); while …
前言
C++ 作为老大哥竟然一直不支持 split ,在看程设模拟的时候发现了可以通过 getline 优雅的实现 split。
代码
vector<string> split(const string& s, char c) { //分隔文件名 vector<string> res; string tmp; stringstream ss(s); while (getline(ss, tmp, c)) res.push_back(tmp); //res保存整体 return res;
}
// std::vector<std::string> split(const std::string& line, char c) {
// std::stringstream stm(line);
// std::vector<std::string> ans;
// std::string tmp;
// while (std::getline(stm, tmp, c)) ans.push_back(tmp);
// return ans;
// }
int main() { // ios::sync_with_stdio(false); // cout.tie(NULL); string s; while (cin >> s) { vector<string> V = split(s, '/'); cout << V.size() << " "; for (auto& it : V) { cout << it << " "; } cout << endl; } return 0;
}
input
a/bb/cc
a/bb/cc/
a/bb/cc-c//c
output
3 a bb cc
3 a bb cc
5 a bb cc-c c
解释
basic_istream& getline( char_type* s, std::streamsize count, char_type delim );
从流释出字符,直至行尾或指定的分隔符 delim
。
参考链接:
文章来源: blog.csdn.net,作者:Anadem,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Aloneingchild/article/details/116600313
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END