std::quoted
提供: cppreference.com
ヘッダ <iomanip> で定義
|
||
template< class CharT > /*unspecified*/ quoted(const CharT* s, |
(1) | (C++14以上) |
template< class CharT, class Traits, class Allocator > /*unspecified*/ quoted(const std::basic_string<CharT, Traits, Allocator>& s, |
(2) | (C++14以上) |
template< class CharT, class Traits> /*unspecified*/ quoted(std::basic_string_view<CharT, Traits> s, |
(3) | (C++17以上) |
template< class CharT, class Traits, class Allocator > /*unspecified*/ quoted(std::basic_string<CharT, Traits, Allocator>& s, |
(4) | (C++14以上) |
CSV や XML で使われるような、引用符付き文字列の挿入および抽出を可能にします。
1-3) 式 out << quoted(s, delim, escape) (ただし
out
は CharT
と等しい char_type
および Traits
と等しい traits_type
(オーバーロード (2-4) の場合) を持つ出力ストリームです) で使用されたとき、以下のように構築される文字のシーケンス seq
を out に挿入する FormattedOutputFunction として動作します。a) まず、文字
delim
がシーケンスに追加されます。b) その後、
s
のすべての文字。 ただし、出力する次の文字が delim
または escape
と等しい (ストリームの traits_type::eq
によって判定されます) 場合は、その前に escape
のコピーを追加します。c) 最後に、
delim
が seq
にもう一度追加されます。
- その後、
seq.size() < out.width()
の場合は、シーケンスの終端 (out.flags()
) にios_base::left
がセットされている場合) またはシーケンスの先頭 (それ以外のすべての場合) のいずれかにフィル文字out.fill()
のコピーをout.width()-seq.size()
個追加します。 - 最後に、 out.rdbuf()->sputn(seq, n) (ただし n=std::max(out.width(), seq.size()) です) を呼んだかのように、結果のシーケンスの各文字を出力し、 std::setw の効果 (もしあれば) を取り消すために out.width(0) を呼びます。
4) (2) と同じですが、さらに式 in >> quoted(s, delim, escape) (ただし
in
は CharT
と等しい char_type
および Traits
と等しい traits_type
を持つ入力ストリームです) で使用されたとき、以下のルールに従って 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 を投げます。
[編集] 例
Run this code
#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]