在线文档教程
C++
输入/输出 | Input/output

std::quoted

科技促进发展::引用

Defined in header
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)

允许插入和提取引用的字符串,例如在CSV或XML中找到的字符串。

1-3%29在表达式中使用时out << quoted(s, delim, escape),在哪里out的输出流。char_type等于CharT对于过载2,traits_type等于Traits,表现为FormattedOutputFunction,插入out一系列字符seq结构如下:

A%29首先,字符delim被添加到序列中。

B%29然后每个字符s,除非要输出的下一个字符等于delim或等于escape%28 As由流%27s确定traits_type::eq%29,然后首先附加一个额外的副本escape

最后C%29,delim附加到seq再来一次,如果seq.size() < out.width(),补充out.width()-seq.size()填充字符的副本out.fill()在序列%28if的末尾ios_base::left设为out.flags()%29或在序列%28的开头,在所有其他情况下%29。最后,输出结果序列中的每个字符,就像通过调用out.rdbuf()->sputn(seq, n),在哪里n=std::max(out.width(), seq.size())和out.width(0)取消…的影响std::setw如果有的话。

4%29在表达式中使用时in >> quoted(s, delim, escape),在哪里in的输入流。char_type等于CharT和traits_type等于Traits,从in,使用std::basic_istream::operator>>,根据下列规则:

如果提取的第一个字符不相等,则为%29delim%28 As由流%27s确定traits_type::eq%29,然后简单地执行in >> s...

如果第一个字符是分隔符%29,则为B%29,否则为%28:

1%29关闭skipws输入流上的标志

2%29通过调用s.clear()

3%29提取字符in并将它们附加到s,但每当escape提取字符,忽略该字符,并将下一个字符追加到s.当提取停止时!in==true或者当一个未逃脱的人delim人物被发现了。

4%29丢弃最后%28未转义%29delim性格。

5%29恢复skipws将输入流标记为其原始值。

参数

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 \

返回值

返回未指定类型的对象,以便所描述的行为发生。

例外

抛出std::ios_base::failure如果operator>>或operator<<扔。

二次

#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 << "read in [" << in << "]\n" << "stored as [" << ss.str() << "]\n"; ss >> std::quoted(out std::cout << "written out [" << out << "]\n"; }

二次

产出:

二次

read in [String with spaces, and embedded "quotes" too] stored as ["String with spaces, and embedded \"quotes\" too"] written out [String with spaces, and embedded "quotes" too]

二次

另见

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/manip/