命名空间
变体
操作

std::quoted

来自 cppreference.cn
< cpp‎ | io‎ | manip
 
 
 
输入/输出 操纵符
浮点数格式化
整数格式化
布尔值格式化
字段宽度和填充控制
其他格式化
空白字符处理
输出刷新
(C++20)  

状态标志操作
时间和货币 I/O
(C++11)
(C++11)
(C++11)
(C++11)
带引号操纵符
quoted
(C++14)
 
定义于头文件 <iomanip>
template< class CharT >

/*unspecified*/ quoted( const CharT* s,

                        CharT delim = CharT('"'), CharT escape = CharT('\\') );
(1) (since C++14)
template< class CharT, class Traits, class Allocator >

/*unspecified*/ quoted( const std::basic_string<CharT, Traits, Allocator>& s,

                        CharT delim = CharT('"'), CharT escape = CharT('\\') );
(2) (since C++14)
template< class CharT, class Traits>

/*unspecified*/ quoted( std::basic_string_view<CharT, Traits> s,

                        CharT delim = CharT('"'), CharT escape = CharT('\\') );
(3) (since C++17)
template< class CharT, class Traits, class Allocator >

/*unspecified*/ quoted( std::basic_string<CharT, Traits, Allocator>& s,

                        CharT delim=CharT('"'), CharT escape=CharT('\\') );
(4) (since C++14)

允许插入和提取带引号的字符串,例如在 CSVXML 中找到的字符串。

1-3) 当在表达式 out << quoted(s, delim, escape) 中使用时,其中 out 是一个输出流,其 char_type 等于 CharT,并且对于重载 (2,3),traits_type 等于 Traits,其行为类似于 FormattedOutputFunction,它将字符序列 seq 插入到 out 中,seq 的构造方式如下:
a) 首先,字符 delim 被添加到序列中。
b) 然后,来自 s 的每个字符,除非要输出的下一个字符等于 delim 或等于 escape(由流的 traits_type::eq 确定),则首先附加一个额外的 escape 副本。
c) 最后,delim 再次附加到 seq
然后,如果 seq.size() < out.width(),则添加 out.width()-seq.size() 个填充字符 out.fill() 的副本,要么在序列的末尾(如果在 out.flags() 中设置了 ios_base::left),要么在序列的开头(在所有其他情况下)。
最后,输出结果序列中的每个字符,如同调用 out.rdbuf()->sputn(seq, n),其中 n=std::max(out.width(), seq.size())out.width(0) 以取消 std::setw 的效果(如果存在)。
4) 当在表达式 in >> quoted(s, delim, escape) 中使用时,其中 in 是一个输入流,其 char_type 等于 CharT,并且 traits_type 等于 Traits,则根据以下规则从 in 中提取字符,使用 std::basic_istream::operator>>
a) 如果提取的第一个字符不等于 delim(由流的 traits_type::eq 确定),则简单地执行 in >> s
b) 否则(如果第一个字符是分隔符)
1) 关闭输入流上的 skipws 标志。
2) 通过调用 s.clear() 清空目标字符串。
3)in 中提取字符并将它们附加到 s,但每当提取到 escape 字符时,它将被忽略,并且下一个字符将附加到 s。当 !in == true 或找到未转义的 delim 字符时,提取停止。
4) 丢弃最后的(未转义的)delim 字符。
5) 将输入流上的 skipws 标志恢复为其原始值。

目录

[编辑] 参数

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)
将参数的格式化表示存储在新字符串中
(函数模板) [编辑]