std::basic_ospanstream<CharT,Traits>::span
來自 cppreference.com
< cpp | io | basic ospanstream
std::span<CharT> span() const noexcept; |
(1) | (C++23 起) |
void span( std::span<CharT> s ) noexcept; |
(2) | (C++23 起) |
2) 使包裝的 std::basic_spanbuf 在 s 引用的緩衝區上執行 I/O 操作。
目錄 |
[編輯] 引數
s | - | std::span 引用用作流的新底層緩衝區的儲存 |
[編輯] 返回值
1) 一個 std::span,根據包裝的 std::basic_spanbuf 的開啟模式,引用底層緩衝區或寫入區域。
2) (無)
[編輯] 示例
執行此程式碼
#include <cassert> #include <iostream> #include <span> #include <spanstream> int main() { char out_buf[16]; std::ospanstream ost{std::span<char>{out_buf}}; ost << "C++" << ' ' << 23 << '\0'; // note explicit null-termination auto sp = ost.span(); assert( sp[0] == 'C' && sp[1] == '+' && sp[2] == '+' && sp[3] == ' ' && sp[4] == '2' && sp[5] == '3' && sp[6] == '\0' ); std::cout << "sp.data(): [" << sp.data() << "]\n"; std::cout << "out_buf: [" << out_buf << "]\n"; // spanstream uses out_buf as internal storage, no allocations assert(static_cast<char*>(out_buf) == sp.data()); const char in_buf[] = "X Y 42"; std::ispanstream ist{std::span<const char>{in_buf}}; assert(static_cast<const char*>(in_buf) == ist.span().data()); char c; ist >> c; assert(c == 'X'); ist >> c; assert(c == 'Y'); int i; ist >> i; assert(i == 42); ist >> i; // buffer is exhausted assert(!ist); }
輸出
sp.data(): [C++ 23] out_buf: [C++ 23]
[編輯] 參閱
根據模式獲取或初始化底層緩衝區 ( std::basic_spanbuf<CharT,Traits> 的公共成員函式) |