std::basic_streambuf<CharT,Traits>::setg
來自 cppreference.com
< cpp | io | basic streambuf
protected: void setg( char_type* gbeg, char_type* gcurr, char_type* gend ); |
||
設定定義獲取區(get area)的各指標的值。
呼叫後,eback() == gbeg、gptr() == gcurr 及 egptr() == gend 均為 true。
若 [
gbeg,
gend)
、[
gbeg,
gcurr)
和 [
gcurr,
gend)
中任意一個不是合法範圍(valid range),則行為未定義。
目錄 |
[編輯] 引數
gbeg | - | 指向獲取區新起始位置的指標 |
gcurr | - | 指向獲取區中新當前字元(“獲取指標”)的指標 |
gend | - | 指向獲取區新結尾位置的指標 |
[編輯] 示例
執行此程式碼
#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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 4023 | C++98 | setg 不要求輸入序列為合法範圍 |
要求 |
[編輯] 參閱
重新定位輸出序列的起始、下一個和結束指標 (受保護成員函式) |