std::basic_streambuf<CharT,Traits>:: setg
|
protected
:
void setg ( char_type * gbeg, char_type * gcurr, char_type * gend ) ; |
||
设置定义获取区域指针的值。
调用后, eback ( ) == gbeg 、 gptr ( ) == gcurr 和 egptr ( ) == gend 均为 true 。
如果
[
gbeg
,
gend
)
、
[
gbeg
,
gcurr
)
和
[
gcurr
,
gend
)
中有任意一个不是
有效范围
,则行为未定义。
目录 |
参数
| gbeg | - | 指向获取区域新起始位置的指针 |
| gcurr | - | 指向获取区域中新当前字符( get pointer )的指针 |
| gend | - | 指向获取区域新结束位置的指针 |
示例
#include <iostream> #include <sstream> class null_filter_buf : public std::streambuf { std::streambuf* src; char ch; // 单字节缓冲区 protected: int underflow() { traits_type::int_type i; while ((i = src->sbumpc()) == '\0') ; // 跳过零值 if (!traits_type::eq_int_type(i, traits_type::eof())) { ch = traits_type::to_char_type(i); setg(&ch, &ch, &ch+1); // 提供一个读取位置 } return i; } public: null_filter_buf(std::streambuf* buf) : src(buf) { setg(&ch, &ch + 1, &ch + 1); // 缓冲区初始为满状态 } }; 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
未要求输入序列必须是有效范围
|
要求必须是有效范围 |
参见
|
重新定位输出序列的起始指针、下一个指针和结束指针
(受保护成员函数) |