Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::quoted

      From cppreference.com
      <cpp‎ |io‎ |manip
       
       
       
      Input/output manipulators
      Floating-point formatting
      Integer formatting
      Boolean formatting
      Field width and fill control
      Other formatting
      Whitespace processing
      Output flushing
      Status flags manipulation
      Time and money I/O
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Quoted manipulator
      quoted
      (C++14)
       
      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(conststd::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 inCSV orXML.

      1-3) When used in an expressionout<< quoted(s, delim, escape), whereout is an output stream withchar_type equal toCharT and, for overloads (2,3),traits_type equal toTraits, behaves as aFormattedOutputFunction, which inserts intoout a sequence of charactersseq constructed as follows:
      a) First, the characterdelim is added to the sequence.
      b) Then every character froms, except if the next character to output equalsdelim or equalsescape (as determined by the stream'straits_type::eq), then first appends an extra copy ofescape.
      c) In the end,delim is appended toseq once more.
      Then, ifseq.size()< out.width(), addsout.width()-seq.size() copies of the fill characterout.fill() either at the end of the sequence (ifios_base::left is set inout.flags()) or at the beginning of the sequence (in all other cases).
      Finally, outputs each character from the resulting sequence as if by callingout.rdbuf()->sputn(seq, n), wheren=std::max(out.width(), seq.size()) andout.width(0) to cancel the effects ofstd::setw, if any.
      4) When used in an expressionin>> quoted(s, delim, escape), wherein is an input stream withchar_type equal toCharT andtraits_type equal toTraits, extracts characters fromin, usingstd::basic_istream::operator>>, according to the following rules:
      a) If the first character extracted does not equaldelim (as determined by the stream'straits_type::eq), then simply performsin>> s.
      b) Otherwise (if the first character is the delimiter):
      1) Turns off theskipws flag on the input stream.
      2) Empties the destination string by callings.clear().
      3) Extracts successive characters fromin and appends them tos, except that whenever anescape character is extracted, it is ignored and the next character is appended tos. Extraction stops when!in==true or when an unescapeddelim character is found.
      4) Discards the final (unescaped)delim character.
      5) Restores theskipws flag on the input stream to its original value.

      Contents

      [edit]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\

      [edit]Return value

      Returns an object of unspecified type such that the described behavior takes place.

      [edit]Exceptions

      Throwsstd::ios_base::failure ifoperator>> oroperator<< throws.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_quoted_string_io201304L(C++14)std::quoted

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <sstream> void default_delimiter(){conststd::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(){constchar delim{'$'};constchar escape{'%'}; conststd::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]

      [edit]See also

      (C++20)
      stores formatted representation of the arguments in a new string
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/manip/quoted&oldid=182106"

      [8]ページ先頭

      ©2009-2025 Movatter.jp