std::basic_streambuf<CharT,Traits>::setg
来自 cppreference.cn
< 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 不要求输入序列为合法范围 |
要求 |
[编辑] 参阅
重新定位输出序列的起始、下一个和结束指针 (受保护成员函数) |