std::basic_istringstream<CharT,Traits,Allocator>::basic_istringstream
来自cppreference.com
< cpp | io | basic istringstream
basic_istringstream() : basic_istringstream(std::ios_base::in) { } |
(1) | (C++11 起) |
(2) | ||
explicit basic_istringstream( std::ios_base::openmode mode = std::ios_base::in ); |
(C++11 前) | |
explicit basic_istringstream( std::ios_base::openmode mode ); |
(C++11 起) | |
explicit basic_istringstream( const std::basic_string<CharT,Traits,Allocator>& str, std::ios_base::openmode mode = std::ios_base::in ); |
(3) | |
basic_istringstream( basic_istringstream&& other ); |
(4) | (C++11 起) |
basic_istringstream( std::ios_base::openmode mode, const Allocator& a ); |
(5) | (C++20 起) |
explicit basic_istringstream( std::basic_string<CharT,Traits,Allocator>&& str, std::ios_base::openmode mode = std::ios_base::in ); |
(6) | (C++20 起) |
template<class SAlloc> basic_istringstream( const std::basic_string<CharT,Traits,SAlloc>& str, |
(7) | (C++20 起) |
template<class SAlloc> basic_istringstream( const std::basic_string<CharT,Traits,SAlloc>& str, |
(8) | (C++20 起) |
template<class SAlloc> explicit basic_istringstream( const std::basic_string<CharT,Traits,SAlloc>& str, |
(9) | (C++20 起) |
构造新的字符串流。
1) 默认构造函数。以默认打开模式构造新的底层字符串设备。
2) 构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(mode | std::ios_base::in) 构造底层
basic_stringbuf
对象。3) 以
str
的副本为底层字符串设备的初始内容。以 basic_stringbuf<Char,Traits,Allocator>(str, mode | std::ios_base::in) 构造底层 basic_stringbuf
对象。4) 移动构造函数。用移动语义,构造拥有
other
的状态的字符串流。5) 构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(mode | std::ios_base::in, a) 构造底层
basic_stringbuf
对象。6) 用
str
移动构造底层字符串设备的内容。以 basic_stringbuf<Char,Traits,Allocator>(std::move(str), mode | std::ios_base::in) 构造底层 basic_stringbuf
对象。7) 构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(str, std::ios_base::in, a) 构造底层
basic_stringbuf
对象。8) 构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(str, mode | std::ios_base::in, a) 构造底层
basic_stringbuf
对象。9) 构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(str, mode | std::ios_base::in) 构造底层
basic_stringbuf
对象。参数
str | - | 用于初始化字符串流内容的字符串 | ||||||||||||||
mode | - | 指定流打开模式。它是位掩码类型,定义下列常量:
| ||||||||||||||
other | - | 用作源的另一字符串流 |
注解
在短的循环中,例如在用于字符串转换时构造单次使用的 basic_istringstream
对象,开销可能显著高于调用 str 并复用同一对象。
示例
运行此代码
#include <iostream> #include <sstream> int main() { // 默认构造函数(输入/输出流) std::stringstream buf1; buf1 << 7; int n = 0; buf1 >> n; std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n'; // 输入流 std::istringstream inbuf("-10"); inbuf >> n; std::cout << "n = " << n << '\n'; // 输出流在 append 模式 (C++11) std::ostringstream buf2("test", std::ios_base::ate); buf2 << '1'; std::cout << buf2.str() << '\n'; }
输出:
buf1 = 7 n = 7 n = -10 test1
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
DR | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
P0935R0 | C++11 | 默认构造函数曾为 explicit | 使之为隐式 |
参阅
获取或设置底层字符串设备对象的内容 (公开成员函数) | |
构造一个 basic_stringbuf 对象 ( std::basic_stringbuf<CharT,Traits,Allocator> 的公开成员函数) |