定义于头文件 <sstream>
|-------------------------------------------------------------------------------------------------------------------------------------------|---|-----------|
| template< class CharT, class Traits = std::char_traits<CharT> > class basic_ostringstream; | | (C++11 前) |
| template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> > class basic_ostringstream; | | (C++11 起) |
类模板 std::basic_ostringstream
实现基于字符串的流上的输入与输出操作。它等效地存储一个 std::basic_string 的实例,并在其上进行输出操作。
在低层,该类实际上包装 std::basic_stringbuf 的未处理字符串设备到 std::basic_ostream 的高层接口中。提供到独有 std::basic_stringbuf
成员的完整接口。
亦定义对常用字符类型的二个特化:
|------------------|--------------------------------|
| 类型 | 定义 |
| ostringstream
| basic_ostringstream<char> |
| wostringstream
| basic_ostringstream<wchar_t> |
成员函数
构造字符串流
std::basic_ostringstream<CharT,Traits,Allocator>::basic_ostringstream
|-----------------------------------------------------------------------------------------------------------------------------------|-----|-----------|-----------|
| basic_ostringstream() : basic_ostringstream(ios_base::out) { } | (1) | (C++11 起) |
| explicit basic_ostringstream( ios_base::openmode mode = ios_base::out ); | (2) | (C++11 前) |
| explicit basic_ostringstream( ios_base::openmode mode ); | (2) | (C++11 起) |
| explicit basic_ostringstream( const std::basic_string<CharT,Traits,Allocator>& str, ios_base::openmode mode = ios_base::out ); | (2) | (3) | |
| basic_ostringstream( basic_ostringstream&& other ); | (2) | (4) | (C++11 起) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
构造新的字符串流。
-
默认构造函数。以默认打开模式构造新的底层字符串设备。
-
构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(mode | ios_base::out) 构造底层
basic_stringbuf
对象。 -
以
str
的副本为底层字符串设备的初始内容。以 basic_stringbuf<Char,Traits,Allocator>(str, mode | ios_base::out) 构造底层basic_stringbuf
对象。 -
移动构造函数。用移动语义,构造拥有
other
的状态的字符串流。
参数
|-------|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| str | - | 用于初始化字符串流内容的字符串 |
| mode | - | 指定流打开模式。它是位掩码类型,定义下列常量: |--------|-------------| | 常量 | 解释 | | app | 每次写入前寻位到流结尾 | | binary | 以二进制模式打开 | | in | 为读打开 | | out | 为写打开 | | trunc | 在打开时舍弃流的内容 | | ate | 打开后立即寻位到流结尾 | |
| other | - | 用作源的另一字符串流 |
注意
在短的循环中,例如在用于字符串转换时构造单次使用的 basic_ostringstream
对象,开销可能显著高于调用 str 并复用同一对象。
调用示例
#include <sstream>
#include <string>
#include <iostream>
int main()
{
//1) 默认构造函数。以默认打开模式构造新的底层字符串设备。
std::basic_ostringstream<int> basic_ostringstream1;
//2) 构造新的底层字符串设备。
//以 basic_stringbuf<Char,Traits,Allocator>(mode | ios_base::out)
//构造底层 basic_stringbuf 对象。
std::basic_ostringstream<char> basic_ostringstream2(std::ios_base::out);
//3) 以 str 的副本为底层字符串设备的初始内容。
std::string string1 = "I am a handsome programmer";
std::basic_ostringstream<char>
basic_ostringstream3(string1, std::ios_base::out | std::ios_base::app);
std::cout << "basic_ostringstream3: " << basic_ostringstream3.str() << std::endl;
basic_ostringstream3 << "GGX";
std::cout << "basic_ostringstream3: " << basic_ostringstream3.str() << std::endl;
std::cout << std::endl;
//4) 移动构造函数。用移动语义,构造拥有 other 的状态的字符串流。
std::basic_ostringstream<char> basic_ostringstream4(std::move(basic_ostringstream3));
std::cout << "basic_ostringstream4: " << basic_ostringstream4.str() << std::endl;
return 0;
}