Namespaces
Variants

std::ranges::views::istream, std::ranges::basic_istream_view, std::ranges::istream_view, std::ranges::wistream_view

来自 zh.cppreference.net
 
 
范围库
范围适配器
 
 
定义于头文件 <ranges>
template< std::movable Val, class CharT,
          class Traits = std::char_traits<CharT> >
    requires std::default_initializable<Val> &&
             /*stream-extractable*/<Val, CharT, Traits>
class basic_istream_view
    : public ranges::view_interface<basic_istream_view<Val, CharT, Traits>>
(1) (自C++20起)
辅助模板
template< class Val >
using istream_view = ranges::basic_istream_view<Val, char>;
(2) (自C++20起)
template< class Val >
using wistream_view = ranges::basic_istream_view<Val, wchar_t>;
(3) (自C++20起)
定制点对象
namespace views {
    template< class T >
    constexpr /* unspecified */ istream = /* unspecified */;
}
(4) (自C++20起)
辅助概念
template< class Val, class CharT, class Traits >
concept /*stream-extractable*/ =
    requires(std::basic_istream<CharT, Traits>& is, Val& t) {
        is >> t;
    };
(5) (仅用于说明*)
1) 一个范围工厂,通过重复调用 operator>> 来生成元素序列。
2,3) 针对字符类型 char 和 wchar_t 的便利别名模板。
4) views::istream<T>(e) 是 表达式等价于 ranges::basic_istream_view<T, typename U::char_type, typename U::traits_type>(e) 对于任何合适的子表达式 e,其中 U 是 std::remove_reference_t<decltype(e)>。
如果 U 不是公开且明确地从 std::basic_istream<typename U::char_type, typename U::traits_type> 派生的,则程序非良构,这可能导致 替换失败。
5) 仅用于说明的概念 /*stream-extractable*/<Val, CharT, Traits> 在类型 Val 的左值可以从类型 std::basic_istream<CharT, Traits> 的左值中提取时得到满足。

的迭代器类型 basic_istream_view 是只移动的:它不满足 LegacyIterator 要求,因此无法与 C++20 之前的 算法。

定制点对象

名称 views::istream<T> 表示一个 定制点对象 ,它是一个常量 函数对象 ,具有 字面量 类型的 semiregular 类类型。详情参见 定制点对象 。

数据成员

成员 描述
std::basic_istream<CharT, Traits>* stream_ 指向输入流的指针
(仅用于阐释的成员对象*)
Val value_ 存储的值
(仅用于阐释的成员对象*)

成员函数

构造 basic_istream_view
(公开成员函数)
返回迭代器
(公开成员函数)
返回 std::default_sentinel
(公开成员函数)
继承自 ranges::view_interface
(C++23)
返回指向范围起始的常量迭代器
(公开成员函数 of std::ranges::view_interface<D>)
(C++23)
返回范围常量迭代器的哨位
(公开成员函数 of std::ranges::view_interface<D>)

尽管 basic_istream_view 派生自 ranges::view_interface,但它不能使用任何继承的成员函数。

(C++23 前)

std::ranges::basic_istream_view:: basic_istream_view

constexpr explicit
basic_istream_view ( std:: basic_istream < CharT, Traits > & stream ) ;
(C++20 起)

用 std:: addressof ( stream ) 初始化 stream_ ,并对 value_ 进行值初始化。

std::ranges::basic_istream_view:: begin

constexpr auto begin ( ) ;
(C++20 起)

等价于 * stream_ >> value_  ; return iterator  { * this } ; 。

std::ranges::basic_istream_view:: end

constexpr std:: default_sentinel_t end ( ) const noexcept ;
(C++20 起)

返回 std:: default_sentinel 。

嵌套类

basic_istream_view 的迭代器类型
( 仅用于说明的成员类* )

示例

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <ranges>
#include <sstream>
#include <string>
int main()
{
    auto words = std::istringstream{"today is yesterday’s tomorrow"};
    for (const auto& s : std::views::istream<std::string>(words))
        std::cout << std::quoted(s, '/') << ' ';
    std::cout << '\n';
    auto floats = std::istringstream{"1.1  2.2\t3.3\v4.4\f55\n66\r7.7  8.8"};
    std::ranges::copy
    (
        std::views::istream<float>(floats),
        std::ostream_iterator<float>{std::cout, ", "}
    );
    std::cout << '\n';
}

输出:

缺陷报告

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

DR 应用于 已发布的行为 正确行为
LWG 3568 C++20 P2325R3 意外地使存储值默认初始化 恢复为值初始化
P2325R3 C++20 默认构造函数被提供为
view必须为default_initializable
,连同
要求一起删除
P2432R1 C++20 ranges::istream_view是一个函数模板
并且未遵循命名约定
改为别名模板;
添加了自定义点对象

另见

从 std::basic_istream
读取的输入迭代器(类模板)