std::wbuffer_convert<Codecvt,Elem,Tr>::rdbuf

来自cppreference.com
定义于头文件 <locale>
std::streambuf* rdbuf() const;
(1)
std::streambuf* rdbuf( std::streambuf* bytebuf );
(2)

1) 返回指向底层字节流的指针。

2) 以 bytebuf 替换关联字节流。

返回值

1) 当前的底层字节流

2) 先前的字节流

示例

#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
int main()
{
    // 转换 UTF-8 到 UCS4
    std::stringbuf utf8buf(u8"z\u00df\u6c34\U0001d10b");  // 或 u8"zß水𝄋"
                       // 或 "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv(&utf8buf);
    std::wistream ucsbuf(&conv);
    std::cout << "Reading from a UTF-8 stringbuf via wbuffer_convert:\n";
    for(wchar_t c; ucsbuf.get(c); )
        std::cout << std::hex << std::showbase << c << '\n';
 
    // 复用同一 wbuffer_convert ,以处理 UCS4 到 UTF-8 的输出
    conv.rdbuf(std::cout.rdbuf());
    std::wostream out(&conv);
    std::cout << "Sending UCS4 data to std::cout via wbuffer_convert:\n";
    out << L"z\u00df\u6c34\U0001d10b\n";
}

输出:

Reading from a UTF-8 stringbuf via wbuffer_convert:
0x7a
0xdf
0x6c34
0x1d10b
Sending UCS4 data to std::cout via wbuffer_convert:
zß水𝄋

参阅

构造新的 wbuffer_convert
(公开成员函数)