std:: quoted
|
定义于头文件
<iomanip>
|
||
|
template
<
class
CharT
>
/*未指定类型*/
quoted
(
const
CharT
*
s,
|
(1) | (C++14 起) |
|
template
<
class
CharT,
class
Traits,
class
Allocator
>
/*未指定类型*/
quoted
(
const
std::
basic_string
<
CharT, Traits, Allocator
>
&
s,
|
(2) | (C++14 起) |
|
template
<
class
CharT,
class
Traits
>
/*未指定类型*/
quoted
(
std::
basic_string_view
<
CharT, Traits
>
s,
|
(3) | (C++17 起) |
|
template
<
class
CharT,
class
Traits,
class
Allocator
>
/*未指定类型*/
quoted
(
std::
basic_string
<
CharT, Traits, Allocator
>
&
s,
|
(4) | (C++14 起) |
允许插入和提取带引号的字符串,例如在 CSV 或 XML 中常见的格式。
out
是
char_type
等于
CharT
的输出流,且对于重载 (2,3),
traits_type
等于
Traits
,其行为表现为一个
格式化输出函数
,会向
out
插入按如下方式构造的字符序列
seq
:
seq
中。
- 然后,如果 seq. size ( ) < out. width ( ) ,则添加 out. width ( ) - seq. size ( ) 个填充字符 out. fill ( ) 的副本到序列末尾(若 ios_base :: left 在 out. flags ( ) 中被设置)或序列开头(其他所有情况)。
- 最后,从结果序列中输出每个字符,如同调用 out. rdbuf ( ) - > sputn ( seq, n ) ,其中 n = std:: max ( out. width ( ) , seq. size ( ) ) ,并通过 out. width ( 0 ) 取消 std::setw 可能产生的效果。
in
是
char_type
等于
CharT
且
traits_type
等于
Traits
的输入流,根据以下规则使用
std::basic_istream::operator>>
从
in
提取字符:
traits_type::eq
判定),则直接执行
in
>>
s
。
in
中提取连续字符并追加到
s
,但当提取到
escape
字符时,该字符将被忽略,并将下一个字符追加到
s
。提取在满足
!
in
==
true
或遇到未转义的
delim
字符时停止。
目录 |
参数
| s | - | 要插入或提取的字符串 |
| delim | - | 用作分隔符的字符,默认为 " |
| escape | - | 用作转义字符的字符,默认为 \ |
返回值
返回一个未指定类型的对象,使得所描述的行为得以发生。
异常
若 operator >> 或 operator << 抛出异常,则抛出 std::ios_base::failure 。
备注
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_quoted_string_io
|
201304L
|
(C++14) |
std::quoted
|
示例
#include <iomanip> #include <iostream> #include <sstream> void default_delimiter() { const std::string in = "std::quoted() quotes this string and embedded \"quotes\" too"; std::stringstream ss; ss << std::quoted(in); std::string out; ss >> std::quoted(out); std::cout << "Default delimiter case:\n" "read in [" << in << "]\n" "stored as [" << ss.str() << "]\n" "written out [" << out << "]\n\n"; } void custom_delimiter() { const char delim{'$'}; const char escape{'%'}; const std::string in = "std::quoted() quotes this string and embedded $quotes$ $too"; std::stringstream ss; ss << std::quoted(in, delim, escape); std::string out; ss >> std::quoted(out, delim, escape); std::cout << "Custom delimiter case:\n" "read in [" << in << "]\n" "stored as [" << ss.str() << "]\n" "written out [" << out << "]\n\n"; } int main() { default_delimiter(); custom_delimiter(); }
输出:
Default delimiter case: read in [std::quoted() quotes this string and embedded "quotes" too] stored as ["std::quoted() quotes this string and embedded \"quotes\" too"] written out [std::quoted() quotes this string and embedded "quotes" too] Custom delimiter case: read in [std::quoted() quotes this string and embedded $quotes$ $too] stored as [$std::quoted() quotes this string and embedded %$quotes%$ %$too$] written out [std::quoted() quotes this string and embedded $quotes$ $too]
参见
|
(C++20)
|
将参数的格式化表示存储于新字符串中
(函数模板) |