让我来处理一个字符串:
string tstring = "Some arbitrarily long string which has \"double quotes\" which has to be printed verbatim";
我尝试使用stringstreams和quoted来提取单词。
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
会移除引号。请参见http://en.cppreference.com/w/cpp/io/manip/quoted。`quoted`旨在将带引号的子字符串视为单个项,忽略空格。 - 3Davequoted
的定义似乎表明,在像stream >> std::quoted(temp)
这样的输入中,转义引号应该被保留。你能否发布一个完整、可编译的简短示例? - 3Dave