c++11 标准模板(STL)(std::basic_ostringstream)(四)

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

|-------------------------------------------|---|-----------|
| void swap( basic_ostringstream& other ); | | (C++11 起) |

交换流与 other 的状态。

通过调用 basic_ostream<CharT, Traits>::swap(other) 和 rdbuf()->swap(*other.rdbuf()) 进行。

参数

|-------|---|---------|
| other | - | 要交换状态的流 |

返回值

(无)

调用示例

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    //3) 以 str 的副本为底层字符串设备的初始内容。
    std::string string1 = "I am a handsome programmer";
    std::basic_ostringstream<char>
    basic_ostringstream1(string1, std::ios_base::out);
    std::cout << "basic_ostringstream1: "
              << basic_ostringstream1.str() << std::endl;
    std::cout << "basic_ostringstream1 rdstate: ";
    std::cout << basic_ostringstream1.rdstate() << std::endl;

    std::string string2 = "I am a super handsome guy";
    std::basic_ostringstream<char>
    basic_ostringstream2(string2, std::ios_base::out);
    std::cout << "basic_ostringstream2: "
              << basic_ostringstream2.str() << std::endl;
    std::cout << "basic_ostringstream2 rdstate: ";
    std::cout << basic_ostringstream2.rdstate() << std::endl;
    std::cout << std::endl;

    //交换流与 other 的状态。
    //通过调用 basic_istream<CharT, Traits>::swap(other)
    //和 rdbuf()->swap(*other.rdbuf()) 进行。
    basic_ostringstream1.swap(basic_ostringstream2);
    std::cout << "after swap: " << std::endl;

    std::cout << "basic_ostringstream1: "
              << basic_ostringstream1.str() << std::endl;
    std::cout << "basic_ostringstream1 rdstate: ";
    std::cout << basic_ostringstream1.rdstate() << std::endl;

    std::cout << "basic_ostringstream2: "
              << basic_ostringstream2.str() << std::endl;
    std::cout << "basic_ostringstream2 rdstate: ";
    std::cout << basic_ostringstream2.rdstate() << std::endl;

    return 0;
}

非成员函数

特化 std::swap 算法

std::swap(std::basic_ostringstream)

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| template< class CharT, class Traits, class Alloc > void swap( std::basic_ostringstream<CharT,Traits,Alloc>& lhs, std::basic_ostringstream<CharT,Traits,Alloc>& rhs ); |

为 std::basic_ostringstream 特化 std::swap 算法。交换 lhsrhs 的状态。等效地调用 lhs.swap(rhs) 。

参数

|----------|---|---------|
| lhs, rhs | - | 要交换状态的流 |

返回值

(无)

异常

(无)

调用示例

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    //3) 以 str 的副本为底层字符串设备的初始内容。
    std::string string1 = "I am a handsome programmer";
    std::basic_ostringstream<char>
    basic_ostringstream1(string1, std::ios_base::out);
    std::cout << "basic_ostringstream1: "
              << basic_ostringstream1.str() << std::endl;
    std::cout << "basic_ostringstream1 rdstate: ";
    std::cout << basic_ostringstream1.rdstate() << std::endl;

    std::string string2 = "I am a super handsome guy";
    std::basic_ostringstream<char>
    basic_ostringstream2(string2, std::ios_base::out);
    std::cout << "basic_ostringstream2: "
              << basic_ostringstream2.str() << std::endl;
    std::cout << "basic_ostringstream2 rdstate: ";
    std::cout << basic_ostringstream2.rdstate() << std::endl;
    std::cout << std::endl;

    //为 std::basic_ostringstream 特化 std::swap 算法。
    //交换 lhs 与 rhs 的状态。等效地调用 lhs.swap(rhs)
    std::swap(basic_ostringstream1, basic_ostringstream2);
    std::cout << "after swap: " << std::endl;

    std::cout << "basic_ostringstream1: "
              << basic_ostringstream1.str() << std::endl;
    std::cout << "basic_ostringstream1 rdstate: ";
    std::cout << basic_ostringstream1.rdstate() << std::endl;

    std::cout << "basic_ostringstream2: "
              << basic_ostringstream2.str() << std::endl;
    std::cout << "basic_ostringstream2 rdstate: ";
    std::cout << basic_ostringstream2.rdstate() << std::endl;

    return 0;
}

输出

相关推荐
Ritsu栗子5 分钟前
代码随想录算法训练营day35
c++·算法
好一点,更好一点15 分钟前
systemC示例
开发语言·c++·算法
卷卷的小趴菜学编程36 分钟前
c++之List容器的模拟实现
服务器·c语言·开发语言·数据结构·c++·算法·list
年轮不改36 分钟前
Qt基础项目篇——Qt版Word字处理软件
c++·qt
玉蜉蝣1 小时前
PAT甲级-1014 Waiting in Line
c++·算法·队列·pat甲·银行排队问题
半盏茶香3 小时前
扬帆数据结构算法之雅舟航程,漫步C++幽谷——LeetCode刷题之移除链表元素、反转链表、找中间节点、合并有序链表、链表的回文结构
数据结构·c++·算法
哎呦,帅小伙哦3 小时前
Effective C++ 规则41:了解隐式接口和编译期多态
c++·effective c++
DARLING Zero two♡4 小时前
【初阶数据结构】逆流的回环链桥:双链表
c语言·数据结构·c++·链表·双链表
9毫米的幻想4 小时前
【Linux系统】—— 编译器 gcc/g++ 的使用
linux·运维·服务器·c语言·c++
Cando学算法4 小时前
Codeforces Round 1000 (Div. 2)(前三题)
数据结构·c++·算法