Namespaces
Variants

std:: quoted

From cppreference.net
< cpp ‎ | io ‎ | manip
定义于头文件 <iomanip>
template < class CharT >

/*未指定类型*/ quoted ( const CharT * s,

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

/*未指定类型*/ quoted ( const std:: basic_string < CharT, Traits, Allocator > & s,

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

/*未指定类型*/ quoted ( std:: basic_string_view < CharT, Traits > s,

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

/*未指定类型*/ quoted ( std:: basic_string < CharT, Traits, Allocator > & s,

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

允许插入和提取带引号的字符串,例如在 CSV XML 中常见的格式。

1-3) 当在表达式 out << quoted ( s, delim, escape ) 中使用时,其中 out char_type 等于 CharT 的输出流,且对于重载 (2,3), traits_type 等于 Traits ,其行为表现为一个 格式化输出函数 ,会向 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 ( ) 的副本到序列末尾(若 ios_base :: left out. flags ( ) 中被设置)或序列开头(其他所有情况)。
最后,从结果序列中输出每个字符,如同调用 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 的输入流,根据以下规则使用 std::basic_istream::operator>> in 提取字符:
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)
将参数的格式化表示存储于新字符串中
(函数模板)