std::quoted(3)
π std::quoted(3) β C++ Standard Library bestie π
okay so bestie, this is the page for std::quoted, from the C++ Standard Library, and it's literally the moment your strings get wrapped in quotes without the whole vibe collapsing. No cap. β¨
π NAME π
std::quoted β std::quoted. That's the name, she's iconic, periodt. π
πΈ Synopsis πΈ
Defined in header <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)
Allows insertion and extraction of quoted strings, such as the ones found in CSV or XML.
1β3) When used in an expression out << quoted(s, delim, escape), where out is an output stream with char_type equal to CharT and, for overloads (2,3), 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
delimis added to the sequence. - b) Then every character from
sis appended β except if the next character to output equalsdelimor equalsescape(as determined by the stream'straits_type::eq), then first an extra copy ofescapeis appended. - c) In the end,
delimis appended toseqonce 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.
- When used in an expression
in >> quoted(s, delim, escape), whereinis an input stream withchar_typeequal toCharTandtraits_typeequal toTraits, extracts characters fromin, usingstd::basic_istream::operator>>, according to the following rules:
- a) If the first character extracted does not equal
delim(as determined by the stream'straits_type::eq), then simply performsin >> s. - b) Otherwise (if the first character is the delimiter):
- Turns off the
skipwsflag on the input stream. - Empties the destination string by calling
s.clear(). - Extracts characters from
inand appends them tos, except that whenever anescapecharacter is extracted, it is ignored and the next character is appended tos. Extraction stops when!in == trueor when an unescapeddelimcharacter is found. - Discards the final (unescaped)
delimcharacter. - Restores the
skipwsflag on the input stream to its original value.
- Turns off the
π¦ 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.
β Notes β
Feature-test macro tea β:
- Feature-test macro:
__cpp_lib_quoted_string_io - Value:
201304L - Std:
(C++14) - Feature:
std::quoted
π Example π
Example time β // Run this code π
// Run this code
#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();
}
Output:
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]
π§ See also π§
formatβ stores formatted representation of the arguments in a new string;(C++20)(function template).
mwah π β source/version: http://cppreference.com, 2024.06.10, page std::quoted(3).
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms