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;
}

输出

相关推荐
水木兰亭1 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
CoderCodingNo2 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法
秋风&萧瑟4 小时前
【C++】C++中的友元函数和友元类
c++
梁诚斌4 小时前
使用OpenSSL接口读取pem编码格式文件中的证书
开发语言·c++
2301_803554528 小时前
c++中的绑定器
开发语言·c++·算法
海棠蚀omo9 小时前
C++笔记-位图和布隆过滤器
开发语言·c++·笔记
消失的旧时光-19439 小时前
c++ 的标准库 --- std::
c++·jni
GiraKoo9 小时前
【GiraKoo】C++11的新特性
c++·后端
不午睡的探索者10 小时前
告别性能瓶颈!Python 量化工程师,进击 C++ 高性能量化交易的“必修课”!
c++·github
OpenC++10 小时前
【C++】观察者模式
c++·观察者模式·设计模式