Namespaces
Variants

std:: istreambuf_iterator

From cppreference.net
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11) (C++14)
(C++14) (C++14)
(C++11) (C++14)
(C++14) (C++14)
(C++17) (C++20)
(C++17)
(C++17)
定义于头文件 <iterator>
template < class CharT, class Traits = std:: char_traits < CharT > >

class istreambuf_iterator
: public std:: iterator < std:: input_iterator_tag ,
CharT, typename Traits :: off_type ,

/* 未指定 */ , CharT >
(C++17 前)
template < class CharT, class Traits = std:: char_traits < CharT > >
class istreambuf_iterator ;
(C++17 起)

std::istreambuf_iterator 是一种单次传递输入迭代器,用于从构造时指定的 std::basic_streambuf 对象中读取连续字符。

默认构造的 std::istreambuf_iterator 被称为 流结束 迭代器。当 std::istreambuf_iterator 到达底层流的末尾时,它将变为与流结束迭代器相等。此时再对其进行解引用或递增操作将引发未定义行为。

std::istreambuf_iterator 具有平凡的复制构造函数、常量表达式默认构造函数和平凡的析构函数。

(since C++11)

目录

成员类型

成员类型 定义
iterator_category std:: input_iterator_tag
value_type CharT
difference_type typename Traits :: off_type
pointer /* unspecified */
reference CharT
char_type CharT
traits_type Traits
int_type typename Traits :: int_type
streambuf_type std:: basic_streambuf < CharT, Traits >
istream_type std:: basic_istream < CharT, Traits >
/* proxy */ 实现定义的类类型。
一个 proxy 对象持有 char_type 字符和 streambuf_type* 指针。
通过 operator* 解引用 proxy 对象将返回存储的字符。
( 仅用于说明的成员类型* )

成员类型 iterator_category value_type difference_type pointer reference 需要通过继承自 std:: iterator < std:: input_iterator_tag , CharT, typename Traits :: off_type , /* 未指定 */ , CharT > 来获取。

(C++17 前)

成员类型 pointer 通常为 CharT* (参见 下文 )。

成员函数

构造新的 istreambuf_iterator
(公开成员函数)
(destructor)
(隐式声明)
销毁 istreambuf_iterator
(公开成员函数)
获取当前字符的副本
(公开成员函数)
推进迭代器
(公开成员函数)
检查两个 istreambuf_iterator 是否均为流结束或均有效
(公开成员函数)

非成员函数

(在 C++20 中移除)
比较两个 istreambuf_iterator
(函数模板)

注释

LWG问题659的决议引入了 operator - > 。预期对于给定的 std::istreambuf_iterator i ,表达式 ( * i ) . m i - > m 应具有相同效果。

然而,该决议并未对其行为提供正式规范。因此实际实现存在差异,包括返回 nullptr 、返回临时对象地址,或根本不提供该成员。其预期行为几乎无法实现,最终通过 LWG issue 2790 的决议被移除。

LWG问题 659 的解决方案同时将成员类型 pointer 设为未指定类型,以便允许 operator-> 返回代理对象。这是为了在 CharT 不是类类型时仍能编译通过 operator-> 操作。

示例

#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
int main()
{
    // 典型用例:表示为迭代器对的输入流
    std::istringstream in{"Hello, world"};
    std::istreambuf_iterator<char> it{in}, end;
    std::string ss{it, end};
    std::cout << "ss has " << ss.size() << " bytes; "
                 "it holds \"" << ss << "\"\n";
    // 单次遍历特性的演示
    std::istringstream s{"abc"};
    std::istreambuf_iterator<char> i1{s}, i2{s};
    std::cout << "i1 returns '" << *i1 << "'\n"
                 "i2 returns '" << *i2 << "'\n";
    ++i1;
    std::cout << "after incrementing i1, but not i2:\n"
                 "i1 returns '" << *i1 << "'\n"
                 "i2 returns '" << *i2 << "'\n";
    ++i2;
    std::cout << "after incrementing i2, but not i1:\n"
                 "i1 returns '" << *i1 << "'\n"
                 "i2 returns '" << *i2 << "'\n";
}

输出:

ss has 12 bytes; it holds "Hello, world"
i1 returns 'a'
i2 returns 'a'
after incrementing i1, but not i2:
i1 returns 'b'
i2 returns 'b'
after incrementing i2, but not i1:
i1 returns 'c'
i2 returns 'c'

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 659 C++98 1. std::istreambuf_iterator 未包含 operator - >
2. 成员类型 pointer 被指定为 CharT*
1. 已添加
2. 改为未指定
LWG 2790 C++98 LWG 659 添加的 operator - > 不实用 已移除

参见

写入到 std::basic_streambuf 的输出迭代器
(类模板)
std::basic_istream 读取的输入迭代器
(类模板)