命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | io‎ | basic stringbuf
 
 
 
 
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 可用于以可移植的方式在用户提供的 char 数组上实现 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> 的公有成员函数) [编辑]