1.1.字符串流缓冲区String stream buffer
typedef basic_stringbuf <char> stringbuf; 流缓冲区以读取和写入 串对象
1.2.说明:
此类的对象在内部维护一个字符序列,它们用作它们的关联输入序列和/或关联输出序列。
内部序列可以从串 对象,或使用成员复制到一个对象 力量。
继承虚拟成员提供接口,可访问流内部字符序列。流缓冲在此类中被覆盖内容
2.1.成员类型:
char_type char
traits_type char_traits<char>
allocator_type allocator<char>
int_type int
pos_type streampos
off_type streamoff
2.2.成员函数
constructor(构造函数) 构造一个字符串流缓冲区对象(公共成员函数)
str 获取流中全部字符
get/set 获取/设置字符串内容(公共成员函数)
继承公共成员函数:
locales: 语言环境
pubimbue Imbue语言环境(公共成员函数)
getloc 获取当前语言环境(公共成员函数)
缓冲区管理和定位:
pubsetbuf 设置缓冲区数组(公共成员函数)
pubseekoff 将内部位置指针设置为相对位置(公共成员函数)
pubseekpos 将内部位置指针设置为绝对位置(公共成员函数)
pubsync 同步流缓冲区(公共成员函数)
输入函数(get):
in_avail 获取可读取字符数(公共成员函数)
snextc 前进到下一个位置并获得字符(公共成员函数)
sbumpc 获取当前字符并前进到下一个位置(公共成员函数)
sgetc 获取当前字符(公共成员函数)
sgetn 获取字符序列(公共成员函数)
sputbackc 放回字符(公共成员函数)
sungetc 减少当前位置(公共成员函数)
输出函数(输入):
sputc 将字符存储在当前放置位置并增加放置指针(公共成员函数)
sputn 放置字符序列(公共成员函数)
受保护虚函数覆盖 Protected virtual function overrides
setbuf 设置缓冲区(受保护的虚拟成员函数)
seekoff 将位置指针设置为相对位置(受保护的虚拟成员函数)
seekpos 将位置指针设置为绝对位置(受保护的虚拟成员函数)
underflow 获取字符(受保护的虚拟成员函数)
pbackfail 放回字符(受保护的虚拟成员函数)
overflow 放置字符(受保护的虚拟成员函数)
该类还继承了其他非虚拟或未覆盖的受保护成员。见基类流缓冲 更多细节。
3.实例:
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
std::stringbuf buffer;
buffer.str("This is test stringbuf");
cout << buffer.str() << endl; //This is test stringbuf
streamsize size = buffer.in_avail();//long long可读字符个数=22
cout << "1.1.size=" << size << endl;
char arr[10] = {0}, arr1[23] = {0};
buffer.sgetn(arr, 10); //得到n个字符 This is te
size = buffer.in_avail(); //12
cout << "1.2.size=" << size << endl;
puts(arr); //从开头放10个字符,尾部换行
//gets_s(arr1, 23); //不要使用
//cout << "arr1=" << arr1 << endl; //This is te孂?
buffer.sgetn(arr1, 22);
cout << "1.3.arr1=" << arr1 << endl;
buffer.str("This is test stringbuf");
buffer.sgetn(arr1, 22);
cout << "1.4.arr1=" << arr1 << endl;//This is test stringbuf
buffer.str("This is test stringbuf");
buffer.sputn("Tom", 3);
char arr2[25] = { 0 };
buffer.sgetn(arr2, 25);
cout << "1.5.arr2=" << arr2 << endl;//Toms is test stringbuf
buffer.str("This is test stringbuf");
cout << "1.6.";
while (buffer.snextc() != EOF) //移动到下一个位置
{
char cc = buffer.sgetc(); //返回当前位置字符
cout << cc << ";";
}
cout << endl;/*h;i;s; ;i;s; ;t;e;s;t; ;s;t;r;i;n;g;b;u;f; */
size = buffer.in_avail();
cout << "1.7.size=" << size << endl;//0
buffer.str("This is test stringbuf");
cout << "1.8.";
while (buffer.sgetc() != EOF) //移动到下一个位置
{
char cc = buffer.sbumpc(); //返回当前位置字符
cout << cc << ";";
}
cout << endl;
size = buffer.in_avail();/*T; h; i; s; ; i; s; ; t; e; s; t; ; s; t; r; i; n; g; b; u; f;*/
cout << "1.9.size=" << size << endl;//0
buffer.str("This is test stringbuf");
}
//http://www.cplusplus.com/reference/sstream/stringbuf/