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
是一個輸出流,其 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
,它使用 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。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__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) |
將引數的格式化表示儲存在新字串中 (函式模板) |