【发布时间】:2017-03-28 05:49:41
【问题描述】:
给我一个字符串:
string tstring = "Some arbitrarily long string which has \"double quotes\" which has to be printed verbatim";
我尝试使用字符串流和引号来提取单词
stringstream stream(tstring);
string tepm;
while(stream >> std::quoted(temp))
cout << temp << endl;
但上面跳过了引用字符串中的引号
Some
arbitrarily
.
.
double quotes
.
.
verbatim
我希望带引号的字符串逐字打印
Some
arbitrarily
.
.
"double quotes"
.
.
verbatim
我如何使用引用的函数来做到这一点,或者如果不可能,有没有更好的方法来做到这一点(当然除了逐字阅读并自己完成所有工作)
编辑:
这是所要求的 MCVE
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main(){
string sspace = "Hi this is \"Real Madrid\"";
stringstream stream(sspace);
string fpart;
while(stream >> quoted(fpart)){
cout << fpart << endl;
}
return 0;
}
【问题讨论】:
-
std::quoted正在删除引号。见en.cppreference.com/w/cpp/io/manip/quoted。quoted旨在将引用的子字符串视为单个项目,忽略空格。 -
@DavidLively 我知道引用是删除引号因此输出,有没有办法找出它是否被引用并保留引号和字符串?
-
Hmmm.. 实际上,
quoted的定义似乎是说,当在像stream >> std::quoted(temp)这样的输入中使用时,应该保留转义的引号。您能否发布一个完整的、可编译的简短示例? -
@DavidLively 如果你想检查,我已经添加了 MCVE。
标签: c++ string c++14 stringstream