本文介绍了如何使用stringstream来分隔逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
std::string str = "abc def,ghi"; std::stringstream ss(str); string token; while (ss >> token) { printf("%s\n", token.c_str()); }输出为:
abc def,ghi
abc def,ghi
c> stringstream ::>> 运算符可以按空格分隔字符串,但不能以逗号分隔。是否还有修改上述代码,以便我可以得到以下结果?
So the stringstream::>> operator can separate strings by space but not by comma. Is there anyway to modify the above code so that I can get the following result?
输入:abc ,def,ghi
input: "abc,def,ghi"
输出: abc def ghi
output: abc def ghi
推荐答案
#include <iostream> #include <sstream> std::string input = "abc,def,ghi"; std::istringstream ss(input); std::string token; while(std::getline(ss, token, ',')) { std::cout << token << '\n'; }
更多推荐
如何使用stringstream来分隔逗号分隔的字符串
发布评论