迭代器库-流迭代器
迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。
迭代器分类
迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。
迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。
迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。
流迭代器- 写入 std::basic_ostream 的输出迭代器
std::ostream_iterator
template< class T, class CharT = char, | (C++17 前) | |
template< class T, class CharT = char, | (C++17 起) |
std::ostream_iterator
是单趟遗留输出迭代器 (LegacyOutputIterator) ,用 operator<<
写入相继 T
类型对象到为之创建迭代器的 std::basic_ostream 对象。每次写操作后写入可选的分隔字符串。写操作在赋值给迭代器时(无论是否解引用)进行。自增 std::ostream_iterator
是无操作。
典型实现中, std::ostream_iterator
仅有的数据成员是指向关联 std::basic_ostream
的指针和指向分隔字符串首字符的指针。
写入字符时, std::ostreambuf_iterator 更有效率,因为它避免对每个字符构造并析构一次 sentry 对象的开销。
成员类型
成员类型 | 定义 |
iterator_category | std::output_iterator_tag |
value_type | void |
difference_type | void |
pointer | void |
reference | void |
char_type | CharT |
traits_type | Traits |
ostream_type | std::basic_ostream<CharT, Traits> |
要求通过从 std::iterator<std::output_iterator_tag, void, void, void, void> 继承获得成员类型 | (C++17 前) |
写对象到关联的输出序列
std::ostream_iterator<T,CharT,Traits>::operator=
ostream_iterator& operator=( const T& value ); |
插入 value
到关联的流,然后插入分隔符,若在构造时指定它。
若 out_stream
是指向关联 std::basic_ostream 的指针而 delim
是在此对象构造时指定的分隔符,则效果等价于
*out_stream << value;
if(delim != 0)
*out_stream << delim;
return *this;
参数
value | - | 要插入的对象 |
返回值
*this
注意
T
能是任何拥有用户定义 operator<< 的类。
无操作
std::ostream_iterator<T,CharT,Traits>::operator*
ostream_iterator& operator*(); |
不做任何事,提供此函数以满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
它返回迭代器自身,这使得可以用诸如 *iter = value 的代码输出(插入)值到底层的流。
参数
(无)
返回值
*this
无操作
std::ostream_iterator<T,CharT,Traits>::operator++
ostream_iterator& operator++(); | ||
ostream_iterator& operator++( int ); |
不做任何事。提供这些运算符以满足遗留输出迭代器 (LegacyOutputIterator) 的要求。它们使得表达式 *iter++=value 和 *++iter=value 可用于输出(插入)值到底层的流。
参数
(无)
返回值
*this
调用示例
#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 istringstream2("1 2 3 4 5 6 7 8 9 10");
std::cout << "The first Cell cell.x + cell.y > 12 is ";
std::istringstream str2("1 2 3 4 5 6 7 8 9 10");
auto function = [](const Cell & cell)
{
Cell cellt = cell;
cellt += cell;
return cellt;
};
std::ostream_iterator<Cell> ostream_iterator1(std::cout, ", ");
std::fill_n(ostream_iterator1, 5, function(Cell{101, 101}));
std::cout << std::endl;
std::ostream_iterator<Cell> ostream_iterator2(std::cout);
*ostream_iterator2++ = Cell{103, 103};
std::cout << std::endl;
alg(ostream_iterator2, ostream_iterator2);
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
{7,8} == {5,6} false
{7,8} != {5,6} true
istream_iterator2: {7,8}
{7,8} == {7,8} false
{7,8} != {7,8} true