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

输出

相关推荐
zyx没烦恼14 分钟前
Linux 多线程
linux·运维·服务器·开发语言·c++
愚润求学26 分钟前
【C++】模板进阶
c语言·开发语言·c++·笔记·模板
seaeress37 分钟前
opencv(C++)处理图像颜色
c++·人工智能·opencv
xyd陈宇阳42 分钟前
C++ 入门三:函数与模板
开发语言·c++
oioihoii1 小时前
C++23新特性详解:迈向更现代化的C++
开发语言·c++·c++23
darkchink1 小时前
[LevelDB]Block系统内幕解析-元数据块(Meta Block)&元数据索引块(MetaIndex Block)&索引块(Index Block)
android·java·服务器·c语言·数据库·c++·分布式
iFlyCai2 小时前
Xcode警报“Ignoring duplicate libraries: ‘-lc++’” 警报
开发语言·c++
xiecoding.cn2 小时前
Sublime Text使用教程(用Sublime Text编写C语言程序)
c语言·c++·青少年编程·编辑器·sublime text
oioihoii2 小时前
C++23新特性:显式对象形参与显式对象成员函数
开发语言·c++·c++23
埜玊3 小时前
this指针 和 类的继承
c++