std::basic_streambuf<CharT,Traits>::setg
From cppreference.com
< cpp | io | basic streambuf
| protected: void setg( char_type* gbeg, char_type* gcurr, char_type* gend ); |
||
getエリアを定義するポインタの値を設定します。
呼び出し後、eback() = gbeg、gptr() = gcurr、egptr() = gend はすべてtrue になります。
もし [gbeg, gend)、[gbeg, gcurr)、[gcurr, gend) のいずれかが有効な範囲でない場合、動作は未定義です。
目次 |
[編集] パラメータ
| gbeg | - | getエリアの新しい先頭へのポインタ |
| gcurr | - | getエリアの新しい現在の文字(get ポインタ)へのポインタ |
| gend | - | getエリアの新しい末尾へのポインタ |
[編集] 例
このコードを実行
#include <iostream> #include <sstream> class null_filter_buf : public std::streambuf { std::streambuf* src; char ch; // single-byte buffer protected: int underflow() { traits_type::int_type i; while ((i = src->sbumpc()) == '\0') ; // skip zeroes if (!traits_type::eq_int_type(i, traits_type::eof())) { ch = traits_type::to_char_type(i); setg(&ch, &ch, &ch+1); // make one read position available } return i; } public: null_filter_buf(std::streambuf* buf) : src(buf) { setg(&ch, &ch + 1, &ch + 1); // buffer is initially full } }; void filtered_read(std::istream& in) { std::streambuf* orig = in.rdbuf(); null_filter_buf buf(orig); in.rdbuf(&buf); for (char c; in.get(c);) std::cout << c; in.rdbuf(orig); } int main() { char a[] = "This i\0s \0an e\0\0\0xample"; std::istringstream in(std::string(std::begin(a), std::end(a))); filtered_read(in); }
出力
This is an example
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 4023 | C++98 | setg は入力シーケンスに有効な範囲を要求していませんでした |
要求するようになった |
[編集] 関連項目
| 出力シーケンスの先頭、次、および末尾のポインタを再配置する (protected メンバ関数) |