迭代器库-流迭代器
迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。
迭代器分类
迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。
迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。
迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。
写入 std::basic_streambuf 的输出迭代器
std::ostreambuf_iterator
template< class CharT, class Traits = std::char_traits<CharT> > class ostreambuf_iterator : public std::iterator<std::output_iterator_tag, void, void, void, void> | (C++17 前) | |
template< class CharT, class Traits = std::char_traits<CharT> > | (C++17 起) |
std::ostreambuf_iterator
是单趟遗留输出迭代器 (LegacyOutputIterator) ,写入相继元素到为之创建迭代器的 std::basic_streambuf 对象。实际写操作在赋值给迭代器(无论是否解引用)时进行。自增 std::ostreambuf_iterator
是无操作。
典型实现中, std::ostreambuf_iterator
仅有的数据成员是指向关联 std::basic_streambuf
的指针,和指示是否抵达文件尾条件的布尔标志。
成员类型
成员类型 | 定义 |
iterator_category | std::output_iterator_tag |
value_type | void |
difference_type | void |
pointer | void |
reference | void |
char_type | CharT |
traits_type | Traits |
streambuf_type | std::basic_streambuf<CharT, Traits> |
ostream_type | std::basic_ostream<CharT, Traits> |
要求通过从 std::iterator<std::output_iterator_tag, void, void, void, void> 继承获得成员类型 | (C++17 前) |
成员函数
(构造函数) | 构造新的 ostreambuf_iterator (公开成员函数) |
(析构函数) (隐式声明) | 销毁 ostreambuf_iterator (公开成员函数) |
operator= | 写字符到关联的输出序列 (公开成员函数) |
operator* | 无操作 (公开成员函数) |
operator++operator++(int) | 无操作 (公开成员函数) |
failed | 测试是否输出失败 (公开成员函数) |
构造新的 ostreambuf_iterator
std::ostreambuf_iterator<CharT,Traits>::ostreambuf_iterator
ostreambuf_iterator( streambuf_type* buffer ) throw(); | (1) | (C++11 前) |
ostreambuf_iterator( streambuf_type* buffer ) noexcept; | (C++11 起) | |
ostreambuf_iterator( ostream_type& stream ) throw(); | (2) | (C++11 前) |
ostreambuf_iterator( ostream_type& stream ) noexcept; | (C++11 起) |
1) 构造迭代器,将私有的 streambuf_type* 成员设为 buffer
并将 failed() 位设为 false 。若 buffer 是空指针则行为未定义。
2) 同 ostreambuf_iterator(stream.rdbuf()) 。
参数
stream | - | 将关联其 rdbuf() 到此迭代器的输出流 |
buffer | - | 此迭代器要访问的输出流缓冲 |
调用示例
#include <iostream>
#include <sstream>
#include <iterator>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <vector>
#include <time.h>
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::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
std::string string = "This is an example";
std::copy(string.cbegin(), string.cend(), std::ostreambuf_iterator<char>(std::cout));
std::cout << std::endl;
std::basic_filebuf<char> basic_filebuf;
basic_filebuf.open("ostreambuf_iterator.txt", std::ios::out);
std::ostreambuf_iterator<char> ostreambuf_iterator1(&basic_filebuf);
std::ostreambuf_iterator<wchar_t> ostreambuf_iterator2(std::wcout);
*ostreambuf_iterator1 = 'a';
std::copy(string.cbegin(), string.cend(), ostreambuf_iterator1);
*ostreambuf_iterator2 = L'a';
alg(ostreambuf_iterator1, ostreambuf_iterator1);
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return cell;
};
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector<Cell> vector1(6, generate());
std::generate(vector1.begin(), vector1.end(), generate);
//替换容器的内容。1) 以 count 份 value 的副本替换内容。
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout));
std::cout << std::endl;
return 0;
}
输出
This is an example
aalg() called for output iterator
vector1: {114,114}{117,117}{116,116}{110,110}{118,118}{117,117}