首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在C++中读取行中的单词

在C++中读取行中的单词
EN

Stack Overflow用户
提问于 2009-09-27 17:18:48
回答 2查看 7.8K关注 0票数 2

我想知道是否有一种方法可以从一行文本中读取所有的“单词”。

线路看起来像这样: R,4567890,Dwyer,Barb,CSCE 423,CSCE 486

有没有一种方法可以使用逗号作为分隔符,将此行解析为数组或其他内容?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-27 17:23:28

可以,使用std::getline和stringstreams。

代码语言:javascript
运行
复制
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);
票数 12
EN

Stack Overflow用户

发布于 2011-06-27 14:44:27

代码语言:javascript
运行
复制
//#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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1483990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档