迭代器库-迭代器原语
迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。
迭代器分类
迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。
迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。
迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。
用于在容器头部插入的迭代器适配器
std::front_insert_iterator
template< class Container > class front_insert_iterator : public std::iterator< std::output_iterator_tag, void,void,void,void > | (C++17 前) | |
template< class Container > | (C++17 起) |
std::front_insert_iterator
是前附元素到为之构造迭代器的遗留输出迭代器 (LegacyOutputIterator) 。凡在赋值给(无论是否解引用)迭代器时调用容器的 push_front()
成员函数。自增 std::front_insert_iterator
是无操作。
成员类型
成员类型 | 定义 |
iterator_category | std::output_iterator_tag |
value_type | void |
difference_type | void |
pointer | void |
reference | void |
container_type | Container |
要求通过从 std::iterator<std::output_iterator_tag, void, void, void, void> 继承获得成员类型 | (C++17 前) |
成员函数
(构造函数) | 构造新的 front_insert_iterator (公开成员函数) |
operator= | 插入对象到关联的容器 (公开成员函数) |
operator* | 无操作 (公开成员函数) |
operator++operator++(int) | 无操作 (公开成员函数) |
成员对象
成员名 | 定义 |
container (受保护成员对象) | Container* 类型指针 |
无操作
std::front_insert_iterator<Container>::operator*
front_insert_iterator& operator*(); | (C++20 前) | |
constexpr front_insert_iterator& operator*(); | (C++20 起) |
不做任何事,提供此函数以满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
它返回迭代器自身,这使得可以用诸如 *iter = value 的代码输出(插入)值到底层的容器。
参数
(无)
返回值
*this
无操作
std::front_insert_iterator<Container>::operator++
front_insert_iterator& operator++(); | (C++20 前) | |
constexpr front_insert_iterator& operator++(); | (C++20 起) | |
front_insert_iterator& operator++( int ); | (C++20 前) | |
constexpr front_insert_iterator& operator++( int ); | (C++20 起) |
不做任何事。提供这些运算符以满足遗留输出迭代器 (LegacyOutputIterator) 的要求。它们使得表达式 *iter++=value 和 *++iter=value 可用于输出(插入)值到底层的容器。
参数
(无)
返回值
*this
调用示例
#include <iostream>
#include <string>
#include <iterator>
#include <deque>
#include <algorithm>
#include <typeinfo>
#include <time.h>
#include <typeinfo>
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;
}
// 定义一个简单的迭代器适配器
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 mt19937{std::random_device{}()};
srand((unsigned)time(NULL));;
std::cout << std::boolalpha;
auto generate = []()
{
int n = std::rand() % 10 + 100;
Cell cell{n, n};
return cell;
};
std::deque<Cell> deque1;
std::generate_n(std::front_insert_iterator<std::deque<Cell>>(deque1), 6, generate);
std::cout << "deque1 : ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
alg(deque1.rbegin(), deque1.rend());
alg(std::front_insert_iterator<std::deque<Cell>>(deque1), std::front_insert_iterator<std::deque<Cell>>(deque1));
// copy
std::deque<Cell> deque2;
std::front_insert_iterator<std::deque<Cell>> front_insert_iterator2(deque2);
std::cout << "typeid(std::front_insert_iterator<std::deque<Cell>>).name(): "
<< typeid(front_insert_iterator2).name() << std::endl;
//无操作
front_insert_iterator2++;
*front_insert_iterator2;
for (std::deque<Cell>::const_iterator cit = deque1.cbegin(); cit != deque1.cend(); cit++)
{
// 调用 deque2.push_back(*cit)
front_insert_iterator2 = *cit;
}
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "deque1 : ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
return 0;
}
输出
deque1 : {105,105} {104,104} {100,100} {104,104} {101,101} {102,102}
alg() called for random-access iterator
alg() called for output iterator
typeid(std::front_insert_iterator<std::deque<Cell>>).name(): St21front_insert_iteratorISt5dequeI4CellSaIS1_EEE
deque2 : {102,102} {101,101} {104,104} {100,100} {104,104} {105,105}
deque1 : {105,105} {104,104} {100,100} {104,104} {101,101} {102,102}