c++11 标准模板(STL)(std::basic_istringstream)(二)

复制代码
定义于头文件 <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) |

构造新的字符串流。

  1. 默认构造函数。以默认打开模式构造新的底层字符串设备。

  2. 构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(mode | ios_base::out) 构造底层 basic_stringbuf 对象。

  3. str 的副本为底层字符串设备的初始内容。以 basic_stringbuf<Char,Traits,Allocator>(str, mode | ios_base::out) 构造底层 basic_stringbuf 对象。

  4. 移动构造函数。用移动语义,构造拥有 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;
}

输出

相关推荐
Stack Overflow?Tan9044 分钟前
c++实现在同一台主机两个程序实现实时通信
开发语言·c++
@@永恒1 小时前
map&set
c++
小鹏编程2 小时前
【C++教程】C++中的基本数据类型
开发语言·c++·教程·少儿编程
熊峰峰2 小时前
C++第十节:map和set的介绍与使用
开发语言·c++
Antonio9152 小时前
【网络编程】事件选择模型
网络·c++
程序员Linc3 小时前
用OpenCV写个视频播放器可还行?(C++版)
c++·opencv·音视频·opencv 4.11
决斗小饼干3 小时前
并发编程知识总结
c++
Andlin4 小时前
《CMakeList 知识系统学习系列(三):函数和宏》
c++
Forget the Dream4 小时前
设计模式之迭代器模式
java·c++·设计模式·迭代器模式
️Carrie️4 小时前
10.2 继承与多态
c++·多态·继承