std::basic_streambuf::setg
void setg( char_type* gbeg, char_type* gcurr, char_type* gend ); | | |
---|
设置定义GET区域的指针的值。特别是在电话之后eback() == gbeg
,,,gptr() == gcurr
,,,egptr() == gend
...
参数
gbeg | - | pointer to the new beginning of the get area |
---|---|---|
gcurr | - | pointer to the new current character (get pointer) in the get area |
gend | - | pointer to the new end of the get area |
返回值
%280%29
例
二次
#include <iostream>
#include <sstream>
class null_filter_buf : public std::streambuf {
std::streambuf* src;
char ch; // single-byte buffer
protected:
int underflow() {
while( (ch= src->sbumpc()) == '\0') ; // skip zeroes
setg(&ch, &ch, &ch+1); // make one read position available
return ch; // may return EOF
}
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
二次
另见
setp | repositions the beginning, next, and end pointers of the output sequence (protected member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com