iostream - 读取带有嵌入空格的字符串

时间:2010-08-24 13:47:59

标签: c++ iostream

我有一个包含这个记录的文件

123标签现在是所有好人前来援助的时间

总是有一个数字和一些标签后跟一系列单词。我想将数字提取为整数,将标签提取为字符串,将句子提取为字符串。我使用getline和scan加上一些子串愚蠢来做到这一点。

有没有办法做到这一点......

ispringstream iss ("123 Tag Now is the time for all good men to come to the");
integer i;
string tag, sentence;
iss >> i >> tag >> ws >> ???? >> sentence;

即。如果有一些方法可以将白色空间作为终结符,那就太好了。

2 个答案:

答案 0 :(得分:1)

您应该可以分两步完成:

istringstream iss ("123 Tag Now is the time for all good men to come to the");
int i;
std::string tag, sentence;
iss >> i >> tag >> ws;
std::getline(iss, sentence);

答案 1 :(得分:0)

如果没有换行符,

iss >> i >> tag;
getline(iss, sentence);
相关问题