Namespaces
Variants

std::basic_stringbuf<CharT,Traits,Allocator>:: setbuf

From cppreference.net
protected :
virtual std:: basic_streambuf < CharT, Traits > * setbuf ( char_type * s, std:: streamsize n )

如果 s 是空指针且 n 为零,则此函数不产生任何效果。

否则,效果由实现定义:某些实现不执行任何操作,而某些实现会清除当前用作缓冲区的 std::string 成员,并开始使用用户提供的、大小为 n 的字符数组(其首元素由 s 指向)作为缓冲区及输入/输出字符序列。

此函数为受保护的虚函数,只能通过 pubsetbuf() 或从用户定义的派生自 std::basic_stringbuf 的类的成员函数调用。

目录

参数

s - 指向用户提供缓冲区中首个 CharT 的指针或为空
n - 用户提供缓冲区中 CharT 元素的数量或为零

返回值

this

注释

已弃用的流缓冲区 std:: strstreambuf 或 boost.IOStreams 设备 boost::basic_array 可用于以可移植方式在用户提供的字符数组上实现 I/O 缓冲。

示例

测试 stringstream 的 setbuf 功能。

#include <iostream>
#include <sstream>
int main()
{
    std::ostringstream ss;
    char c[1024] = {};
    ss.rdbuf()->pubsetbuf(c, 1024);
    ss << 3.14 << '\n';
    std::cout << c << '\n';
}

输出:

3.14 (on GNU g++/libstdc++ and SunPro C++/roguewave)
<nothing> (on MS Visual Studio 2010, SunPro C++/stlport4, CLang++/libc++)

参见

调用 setbuf ( )
( std::basic_streambuf<CharT,Traits> 的公开成员函数)