Namespaces
Variants

std:: quoted

From cppreference.net
< cpp ‎ | io ‎ | manip
ヘッダーで定義 <iomanip>
template < class CharT >

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

CharT delim = CharT ( '"' ) , CharT escape = CharT ( ' \\ ' ) ) ;
(1) (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) (C++14以降)
template < class CharT, class Traits >

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

CharT delim = CharT ( '"' ) , CharT escape = CharT ( ' \\ ' ) ) ;
(3) (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) (C++14以降)

CSVや CSV または XML で見られるような引用符付き文字列の挿入と抽出を可能にします。

1-3) 式内で使用された場合 out << quoted ( s, delim, escape ) 、ここで out char_type CharT と等しい出力ストリームであり、オーバーロード(2,3)については traits_type Traits と等しい場合、 FormattedOutputFunction として振る舞い、以下のように構築された文字シーケンス seq out に挿入します:
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 フラグを元の値に復元します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters、Return value、Exceptions、Notes、Example、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、IDは一切変更していません - 数値や構造は完全に保持されています - フォーマットとインデントは元のまま維持しています

パラメータ

s - 挿入または抽出する文字列
delim - 区切り文字として使用する文字(デフォルトは "
escape - エスケープ文字として使用する文字(デフォルトは \

戻り値

記述された動作が行われるように、不特定の型のオブジェクトを返します。

例外

std::ios_base::failure が送出される条件: operator >> または operator << が例外を送出した場合。

注記

機能テスト マクロ 標準 機能
__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)
引数のフォーマットされた表現を新しい文字列に格納する
(関数テンプレート)