当 >> 时更改双引号的行为字符串流
这就是我想做的:
假设我有一个字符串流。然后我 << "\"hello world\" Today";
然后当我这样做时,
sstr >> myString1 >> myString2;
我希望 myString1 有“hello world” 对于 myString2 来说,有“今天”
有没有办法(可能使用操纵器)来实现这一点?
谢谢
Here is what I'm trying to do:
say I have a stringstream. Then I << "\"hello world\" today";
then when I do
sstr >> myString1 >> myString2;
I would like myString1 to have "hello world"
and for myString2 to have "today"
Is there a way, possibly with a manipulator, to achieve this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
简短回答: 没有
长回答:
没有任何操纵可以为您做到这一点。
替代答案:
您可以编写自己的类型,该类型可以与流运算符结合使用来完成此任务。
然后就可以正常使用了。
Short Answer: No
Long Answer:
There are no manipulates that will do that for you.
Alternative Answer:
You could write your own type that could be used in conjunction with the stream operators do do this task.
Then it can be used normally.
不,
您要么需要更改流类型(从而更改解析语义),要么使用您自己的字符串类型(从而更改重载操作中的解析语义)。
相反,请考虑编写一个类似于 getline 的函数,该函数从流中解析可能引用的“单词”:
请注意,std::string 的输入已经以空格终止,因此您只需要处理向前查看引用的情况然后处理边缘情况,其中有很多:
“\”你好世界\“今天”
No.
You either need to change the stream type (and hence the parsing semantics) or use your own string type (and hence change the parsing semantics in your overloaded op>>).
Instead, consider writing a function, similar to getline, which parses a possibly-quoted "word" out of a stream:
Note that input into a std::string already terminates at whitespace, so you only need to handle peeking ahead for a quote and then handle edge cases, of which there are plenty:
"\"hello world\"today"
不是直接的,您需要一个“包装”类来终止您想要的地方。
请注意,这是您流式传输到 const 的罕见情况 - 因为即使它是 const,您也可以写入内部 str,这样我就可以传入临时值。
实际上,由于您可能不知道要读什么,您可以简单地将其称为“TokenReader”,它会读取您定义为“令牌”的任何内容。
Not directly, you need a "wrapper" class that terminates where you want it to.
Note that this is a rare occasion where you stream into a const - because you can write to the internal str even though it is const, and this way I can pass in a temporary.
Actually as you probably don't know what you are about to read you could simply call it "TokenReader" which reads whatever you define as a "token".
没有。不是那种特别的方式。您可以为字符串创建一个“强类型定义”,并设计一个新的运算符>>因为它就是这样表现的。
Nope. Not that particular way. You could create a "strong typedef" for string though and design a new operator>> for it that behaves that way.
在 c++14 中
输出:
*来源:https://en.cppreference。 com/w/cpp/io/manip/引用
In c++14
Output:
*Source:https://en.cppreference.com/w/cpp/io/manip/quoted
那是不可能的。这是因为,如果您查看
basic_istream
的operator>>
实现,它实际上是在您执行sstr >> 时调用的。 myString1
,您将在for
循环中看到这一点:意味着,一旦获得“空格”,就退出。因此,一旦获得空间,您就无法继续向
myString1
添加字符。请注意,这是 MSVC++ 实现,但我确信您会在所有实现中找到等效的!
That is not possible. It's because if you look into the implementation of
operator>>
forbasic_istream
which is in fact called when you dosstr >> myString1
, you will see this inside thefor
loop:Means, once you get "space", Quit. So you cannot keep adding characters to your
myString1
once you get space.Note this is MSVC++ implementation, but I'm sure the equivalent you'll found in all implementation!
您可以重载输入流运算符并在其中包含解析语义。
You can overload the input stream operator and include the parsing semantics there.