迭代器库-流迭代器
迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。
迭代器分类
迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。
迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。
迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。
从 std::basic_istream 读取的输入迭代器
std::istream_iterator
template< class T, class CharT = char, | (C++17 前) | |
template< class T, class CharT = char, | (C++17 起) |
std::istream_iterator
是单趟输入迭代器,从为之创建迭代器的 std::basic_istream 对象读取 T
类型的相继对象,通过调用适当的 operator>>
。实际读取操作在自增,而非解引用迭代器时进行。在构造迭代器时读取首个对象。解引用只返回最近读取的对象的副本。
默认构造的 std::istream_iterator
被称为流尾迭代器。合法的 std::istream_iterator
抵达底层流尾时,它变得等于流尾迭代器。解引用和进一步自增它导致未定义行为。
std::istream_iterator
的典型实现保有二个数据成员:指向关联 std::basic_istream 对象的指针和最近读取的 T
类型值。
T
必须满足可默认构造 (DefaultConstructible) 、可复制构造 (CopyConstructible) 和可复制赋值 (CopyAssignable) 要求。
成员类型
成员类型 | 定义 |
iterator_category | std::input_iterator_tag |
value_type | T |
difference_type | Distance |
pointer | const T* |
reference | const T& |
char_type | CharT |
traits_type | Traits |
istream_type | std::basic_istream<CharT, Traits> |
要求通过从 std::iterator<std::input_iterator_tag, T, Distance, const T*, const T&> 继承获得成员类型 | (C++17 前) |
成员函数
(构造函数) | 构造新的 istream_iterator (公开成员函数) |
(析构函数) | 析构 istream_iterator ,包含缓冲的值 (公开成员函数) |
operator*operator-> | 返回当前元素 (公开成员函数) |
operator++operator++(int) | 推进迭代器 (公开成员函数) |
非成员函数
operator==operator!= | 比较两个 istream_iterator (函数模板) |
注意
读字符时, std::istream_iterator
默认跳过空白符(除非用 std::noskipws 或等价物禁用,而 std::istreambuf_iterator 不跳过。另外, std::istreambuf_iterator 更有效率,因为它避免对每个字符构造并析构一次 sentry 对象的开销。
构造新的 istream_iterator
std::istream_iterator<T,CharT,Traits,Distance>::istream_iterator
constexpr istream_iterator(); | (1) | |
istream_iterator( istream_type& stream ); | (2) | |
istream_iterator( const istream_iterator& other ) = default; | (3) |
1) 构造流尾迭代器,值初始化存储的值。若定义 auto x = T(); 中的初始化器是常量初始化器,则此构造函数为 constexpr 。
2) 初始化迭代器并于数据成员中存储 stream
的地址。再从输入流进行首次读取以初始化缓冲数据成员。
3) 构造 other
的副本。若 std::is_trivially_copy_constructible_v<T> 为 true
,则此构造函数为平凡复制构造函数。
参数
stream | - | 用于初始化 istream_iterator 的流 |
other | - | 同类型的另一 istream_iterator |
析构 istream_iterator ,包含缓冲的值
std::istream_iterator<T,CharT,Traits,Distance>::~istream_iterator
~istream_iterator() = default; |
1) 销毁迭代器,包含缓冲的值。
若 | (C++11 起) (C++17 前) |
若 std::is_trivially_destructible_v<T> 为 | (C++17 起) |
调用示例
#include <iostream>
#include <sstream>
#include <iterator>
#include <numeric>
#include <algorithm>
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell(const Cell &cell)
{
x = cell.x;
y = cell.y;
}
Cell(Cell &cell)
{
x = cell.x;
y = cell.y;
cell.x = 0;
cell.y = 0;
}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
std::istream &operator>>(std::istream &is, Cell &cell)
{
is >> cell.x;
is >> cell.y;
return is;
}
// 定义一个简单的迭代器适配器
template<typename _Iterator>
class move_iterator : public std::move_iterator<_Iterator>
{
public:
// 使用基类的构造函数
using std::move_iterator<_Iterator>::move_iterator;
// 可以在此添加其他成员函数,如有需要
};
template< class BDIter >
void alg(BDIter, BDIter, std::input_iterator_tag)
{
//遗留输入迭代器
std::cout << "alg() called for input iterator" << std::endl;
}
template< class BDIter >
void alg(BDIter, BDIter, std::output_iterator_tag)
{
//遗留输出迭代器
std::cout << "alg() called for output iterator" << std::endl;
}
template< class BDIter >
void alg(BDIter, BDIter, std::forward_iterator_tag)
{
//遗留向前迭代器
std::cout << "alg() called for forward iterator" << std::endl;
}
template< class BDIter >
void alg(BDIter, BDIter, std::bidirectional_iterator_tag)
{
//遗留双向迭代器
std::cout << "alg() called for bidirectional iterator" << std::endl;
}
template <class RAIter>
void alg(RAIter, RAIter, std::random_access_iterator_tag)
{
//遗留随机访问迭代器
std::cout << "alg() called for random-access iterator" << std::endl;
}
template< class Iter >
void alg(Iter first, Iter last)
{
alg(first, last,
typename std::iterator_traits<Iter>::iterator_category());
}
int main()
{
std::istringstream istringstream("1 2 3 4 5 6 7 8 9 10");
std::cout << "partial_sum Cell: ";
std::partial_sum(std::istream_iterator<Cell>(istringstream),
std::istream_iterator<Cell>(),
std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::istringstream str2("1 2 3 4 5 6 7 8 9 10");
std::cout << "The first Cell cell.x + cell.y > 12 is ";
auto function = [](const Cell & cell)
{
return cell.x + cell.y > 12;
};
std::istream_iterator<Cell> istream_iterator
= std::find_if(std::istream_iterator<Cell>(str2),
std::istream_iterator<Cell>(), function);
std::cout << *istream_iterator << std::endl;
alg(istream_iterator, istream_iterator);
return 0;
}
输出
partial_sum Cell: {1,2} {4,6} {9,12} {16,20} {25,30}
The first Cell cell.x + cell.y > 12 is {7,8}
alg() called for input iterator