我想知道是否有一种方法可以从一行文本中读取所有的“单词”。
线路看起来像这样: R,4567890,Dwyer,Barb,CSCE 423,CSCE 486
有没有一种方法可以使用逗号作为分隔符,将此行解析为数组或其他内容?
发布于 2009-09-27 17:23:28
可以,使用std::getline和stringstreams。
std::string str = "R,4567890,Dwyer,Barb,CSCE 423,CSCE 486";
std::istringstream iss(str);
std::vector<std::string> words;
while (std::getline(iss, str, ','))
words.push_back(str);发布于 2011-06-27 14:44:27
//#include sstream with angular braces in header files
std::string str = "R,4567890,Dwyer,Barb,CSCE 423,CSCE 486";
std::istringstream iss(str,istringstream:in);
vector<std::string> words;
while (std::getline(iss, str, ','))
words.push_back(str);https://stackoverflow.com/questions/1483990
复制相似问题