简体   繁体   English

从C ++中的行读取单词

[英]read words from line in C++

I was wondering if there was a way to read all of the "words" from a line of text. 我想知道是否有办法从一行文字中读取所有“单词”。

the line will look like this: R,4567890,Dwyer,Barb,CSCE 423,CSCE 486 这条线看起来像这样:R,4567890,Dwyer,Barb,CSCE 423,CSCE 486

Is there a way to use the comma as a delimiter to parse this line into an array or something? 有没有办法使用逗号作为分隔符将此行解析为数组或其他什么?

Yes, use std::getline and stringstreams. 是的,使用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);
//#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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM