std::basic_istringstream<CharT,Traits,Allocator>:: basic_istringstream
| (1) | ||
|
explicit
basic_istringstream
(
std::
ios_base
::
openmode
mode
=
std:: ios_base :: in ) ; |
(C++11 前) | |
|
explicit
basic_istringstream
(
std::
ios_base
::
openmode
mode
)
;
|
(C++11 起) | |
|
basic_istringstream
(
)
: basic_istringstream ( std:: ios_base :: in ) { } |
(2) | (C++11 起) |
|
explicit
basic_istringstream
(
const
std::
basic_string
<
CharT, Traits, Allocator
>
&
str,
|
(3) | |
|
explicit
basic_istringstream
(
std::
basic_string
<
CharT, Traits, Allocator
>
&&
str,
|
(4) | (C++20 起) |
|
basic_istringstream
(
std::
ios_base
::
openmode
mode,
const
Allocator
&
a
)
;
|
(5) | (C++20 起) |
|
template
<
class
SAlloc
>
basic_istringstream
(
const
std::
basic_string
<
CharT, Traits, SAlloc
>
&
str,
|
(6) | (C++20 起) |
|
template
<
class
SAlloc
>
basic_istringstream
(
const
std::
basic_string
<
CharT, Traits, SAlloc
>
&
str,
|
(7) | (C++20 起) |
|
template
<
class
SAlloc
>
explicit
basic_istringstream
|
(8) | (C++20 起) |
|
template
<
class
StringViewLike
>
explicit
basic_istringstream
|
(9) | (C++26 起) |
|
template
<
class
StringViewLike
>
basic_istringstream
(
const
StringViewLike
&
t,
|
(10) | (C++26 起) |
|
template
<
class
StringViewLike
>
basic_istringstream ( const StringViewLike & t, const Allocator & a ) ; |
(11) | (C++26 起) |
|
basic_istringstream
(
basic_istringstream
&&
other
)
;
|
(12) | (C++11 起) |
构造新的字符串流。
给定
-
base_type定义为 std:: basic_istream < CharT, Traits > ,以及 -
buf_type定义为 std:: basic_stringbuf < CharT, Traits, Allocator > ,
基类
std::basic_istream
与
仅用于说明的数据成员
sb
按以下方式完成初始化。
|
重
载 |
std::basic_istream 基类 |
sb
|
|---|---|---|
| (1) | base_type ( std:: addressof ( sb ) ) [1] | buf_type ( mode | std:: ios_base :: in ) |
| (2) | buf_type ( std:: ios_base :: in ) | |
| (3) | buf_type ( str, mode | std:: ios_base :: in ) | |
| (4) | buf_type ( std :: move ( str ) , mode | std:: ios_base :: in ) | |
| (5) | buf_type ( mode | std:: ios_base :: in , a ) | |
| (6) | buf_type ( str, mode | std:: ios_base :: in , a ) | |
| (7) | buf_type ( str, std:: ios_base :: in , a ) | |
| (8) | buf_type ( str, mode | std:: ios_base :: in ) | |
| (9) | std:: addressof ( sb ) | { t, mode | std:: ios_base :: in , Allocator ( ) } |
| (10) | { t, mode | std:: ios_base :: in , a } | |
| (11) | { t, std:: ios_base :: in , a } | |
| (12) | 从 other 的 std::basic_istream 基类移动构造 | 从 other. sb 移动构造 |
- ↑ 在 C++11 之前, std::basic_iostream 基类是通过 base_type ( & sb ) 进行初始化的(适用于重载版本 (1,3) )。
目录 |
参数
| str | - | 用作字符串流初始内容的字符串 | ||||||||||||||||
| t | - | 用作字符串流初始内容的对象(可转换为 std::basic_string_view ) | ||||||||||||||||
| a | - | 用于分配字符串流内容的分配器 | ||||||||||||||||
| mode | - |
指定流打开模式。它是
位掩码类型
,定义了以下常量:
|
||||||||||||||||
| other | - | 用作源的另一字符串流 |
注释
在紧密循环中构造一次性
basic_istringstream
对象(例如用于字符串转换时),其开销可能显著高于调用
str()
复用同一对象。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_sstream_from_string_view
|
202306L
|
(C++26) | std::stringstream 与 std::string_view 的交互操作 ( 9-11 ) |
示例
#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'; // 追加模式的输出流 (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++标准。
| 缺陷报告 | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| P0935R0 | C++11 | 默认构造函数为显式 | 改为隐式 |
另请参阅
|
获取或设置底层字符串设备对象的内容
(公开成员函数) |
|
构造
basic_stringbuf
对象
(
std::basic_stringbuf<CharT,Traits,Allocator>
的公开成员函数)
|