std::quoted

来自cppreference.com
< cpp‎ | io‎ | manip
 
 
 
输入/输出操纵符
浮点格式
原文:
Floating-point formatting
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
整型格式
原文:
Integer formatting
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
布尔格式
原文:
Boolean formatting
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
场的宽度和填充控制
原文:
Field width and fill control
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
其他格式
原文:
Other formatting
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
空白处理
原文:
Whitespace processing
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
输出冲洗
原文:
Output flushing
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
状态标志位操作
原文:
Status flags manipulation
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
时间和金钱I / O
原文:
Time and money I/O
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
(C++11)
(C++11)
(C++11)
(C++11)
 
Template:dcl itemTemplate:dcl itemTemplate:dcl item
定义于头文件 <iomanip>

Allows insertion and extraction of quoted strings, such as the ones found in CSV or XML.

1,2) When used in an expression out << quoted(s, delim, escape), where out is an output stream with char_type equal to CharT and, for overload 2, traits_type equal to Traits, behaves as a FormattedOutputFunction, which inserts into out a sequence of characters seq constructed as follows:
a) First, the character delim is added to the sequence
b) Then every character from s, except if the next character to output equals delim or equals escape (as determined by the stream's traits_type::eq), then first appends an extra copy of escape
c) In the end, delim is appended to seq once more
Then, if seq.size() < out.width(), adds out.width()-seq.size() copies of the fill character out.fill() either at the end of the sequence (if ios_base::left is set in out.flags()) or at the beginning of the sequence (in all other cases).
Finally, outputs each character from the resulting sequence as if by calling out.rdbuf()->sputn(seq, n), where n=std::max(out.width(), seq.size()) and out.width(0) to cancel the effects of std::setw, if any.
3) When used in an expression in >> quoted(s, delim, escape), where in is an input stream with char_type equal to CharT and traits_type equal to Traits, extracts characters from in, using std::basic_istream::operator>>, according to the following rules:
a) If the first character extracted does not equal delim (as determined by the stream's traits_type::eq), then simply performs in >> s.
b) Otherwise (if the first character is the delimiter):
1) Turns off the skipws flag on the input stream
2) Empties the destination string by calling s.clear()
3) Extracts characters from in and appends them to s, except that whenever an escape character is extracted, it is ignored and the next character is appended to s. Extraction stops when !in==true or when an unescaped delim character is found.
4) Discards the final (unescaped) delim character.
5) Restores the skipws flag on the input stream to its original value.

目录

[编辑] Parameters

s - the string to insert or extract
delim - the character to use as the delimiter, defaults to "
escape - the character to use as the escape character, defaults to \

[编辑] Return value

Returns an object of unspecified type such that the described behavior takes place.

[编辑] Exceptions

Throws std::ios_base::failure if operator>> or operator<< throws.

[编辑] Example

#include <iostream>
#include <iomanip>
#include <sstream>
 
int main()
{
    std::stringstream ss;
    std::string in = "String with spaces, and embedded \"quotes\" too";
    std::string out;
 
    ss << std::quoted(in);
    std::cout << "in:  '" << in << "'\n"
              << "stored as '" << ss.str() << "'\n";
 
    ss >> std::quoted(out);
    std::cout << "out: '" << out << "'\n";
}

输出:

in:  'String with spaces, and embedded "quotes" too'
stored as '"String with spaces, and embedded \"quotes\" too"'
out:  'String with spaces, and embedded "quotes" too'

[编辑] See also