迭代器库-迭代器原语
迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。
迭代器分类
迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。
迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。
迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。
解引用结果为右值引用的迭代器适配器
std::move_iterator
template< class Iter > | (C++11 起) |
std::move_iterator
是准确表现为底层迭代器(必须至少是一个遗留输入迭代器 (LegacyInputIterator) )的迭代器适配器,除了解引用会将底层迭代器返回的值转换为右值。若此迭代器用作输入迭代器,则效果是值被移动,而非复制。
比较底层迭代器
operator<(std::move_iterator),
operator<=(std::move_iterator),
operator>(std::move_iterator),
operator>=(std::move_iterator)
template< class Iterator1, class Iterator2 > bool operator==( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 前) | |
template< class Iterator1, class Iterator2 > constexpr bool operator==( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 起) | |
template< class Iterator1, class Iterator2 > bool operator!=( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (2) | (C++17 前) |
template< class Iterator1, class Iterator2 > constexpr bool operator!=( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 起) | |
template< class Iterator1, class Iterator2 > bool operator<( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 前) | |
template< class Iterator1, class Iterator2 > constexpr bool operator<( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 起) | |
template< class Iterator1, class Iterator2 > bool operator<=( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 前) | |
template< class Iterator1, class Iterator2 > constexpr bool operator<=( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 起) | |
template< class Iterator1, class Iterator2 > bool operator>( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 前) | |
template< class Iterator1, class Iterator2 > constexpr bool operator>( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 起) | |
template< class Iterator1, class Iterator2 > bool operator>=( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 前) | |
template< class Iterator1, class Iterator2 > constexpr bool operator>=( const move_iterator<Iterator1>& lhs, const move_iterator<Iterator2>& rhs ); | (C++17 起) |
比较底层迭代器。
参数
lhs, rhs | - | 要比较的迭代器适配器 |
返回值
1) lhs.base() == rhs.base()
2) lhs.base() != rhs.base()
3) lhs.base() < rhs.base()
4) lhs.base() <= rhs.base()
5) lhs.base() > rhs.base()
6) lhs.base() >= rhs.base()
比较底层迭代器与底层哨位
operator==(std::move_iterator<Iter>, std::move_sentinel),
operator!=(std::move_iterator<Iter>, std::move_sentinel)
template<std::sentinel_for<Iter> S> friend constexpr bool operator!=(const move_iterator& i, const std::move_sentinel<S>& s); | (1) | (C++20 起) |
template<std::sentinel_for<Iter> S> friend constexpr bool operator==(const std::move_sentinel<S>& s, const move_iterator& i); | (2) | (C++20 起) |
template<std::sentinel_for<Iter> S> friend constexpr bool operator!=(const move_iterator& i, const std::move_sentinel<S>& s); | (3) | (C++20 起) |
template<std::sentinel_for<Iter> S> friend constexpr bool operator!=(const std::move_sentinel<S>& s, const move_iterator& i); | (4) | (C++20 起) |
比较 move_iterator
与 move_sentinel
。
这些函数对通常无限定或有限定查找不可见,而只能由参数依赖查找在 std::move_iterator<Iter> 为参数的关联类时找到。
参数
i | - | std::move_iterator<Iter> |
s | - | std::move_sentinel<S> ,其中 S 实现 std::sentinel_for<Iter> |
返回值
1-2) i.base() == s.base()
3-4) !(i == s)
令迭代器前进
operator+(std::move_iterator)
template< class Iter > move_iterator<Iter> | (C++17 前) | |
template< class Iter > constexpr move_iterator<Iter> | (C++17 起) |
返回增加了 n
的 it
。
参数
n | - | 增加迭代器所用的位置数 |
it | - | 要增加的迭代器适配器 |
返回值
增加的迭代器,即 move_iterator<Iter>(it.base() + n)
计算两个迭代器适配器间的距离
operator-(std::move_iterator)
template< class Iterator1, class Iterator2 > auto operator-( const move_iterator<Iterator1>& lhs, | (C++11 起) (C++17 前) | |
template< class Iterator1, class Iterator2 > constexpr auto operator-( const move_iterator<Iterator1>& lhs, | (C++17 起) |
返回二个迭代器适配器间的距离。
参数
lhs, rhs | - | 要计算距离的迭代器适配器 |
返回值
lhs.base() - rhs.base()
调用示例
#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 &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(6);
std::generate(deque1.begin(), deque1.end(), 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());
typedef std::deque<Cell>::iterator iterator_t;
move_iterator<iterator_t> move_begin(deque1.begin());
move_iterator<iterator_t> move_end(deque1.end());
// std::cout << typeid(move_begin.base()).name() << " : " << *(move_begin.base()) << std::endl;
for (move_iterator<std::deque<Cell>::iterator>::iterator_type rit
= move_begin.base(); rit != move_end.base(); rit++)
{
std::cout << *rit << " ";
}
std::cout << std::endl;
move_iterator<std::deque<Cell>::iterator> move_iterator1 = move_begin;
std::cout << typeid(move_iterator1.base()).name() << " : " << *(move_iterator1.base()) << std::endl;
//1) lhs.base() == rhs.base()
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " == move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 == move_begin) << std::endl;
//1-2) 分别前自增或前自减一。
++move_iterator1;
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " == move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 == move_begin) << std::endl;
//2) lhs.base() != rhs.base()
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " != move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 != move_begin) << std::endl;
//1-2) 分别前自增或前自减一。
++move_iterator1;
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " != move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 != move_begin) << std::endl;
//3) lhs.base() > rhs.base()
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " > move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 > move_begin) << std::endl;
//4) lhs.base() >= rhs.base()
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " >= move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 >= move_begin) << std::endl;
//5) lhs.base() < rhs.base()
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " < move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 < move_begin) << std::endl;
// 6) lhs.base() <= rhs.base()
std::cout << "move_iterator1: " << *move_iterator1.base()
<< " <= move_begin: " << *move_begin.base() << " ";
std::cout << (move_iterator1 <= move_begin) << std::endl;
return 0;
}
输出
deque1 : {100,100} {104,104} {104,104} {105,105} {100,100} {100,100}
alg() called for random-access iterator
{100,100} {104,104} {104,104} {105,105} {100,100} {100,100}
St15_Deque_iteratorI4CellRS0_PS0_E : {100,100}
move_iterator1: {100,100} == move_begin: {100,100} true
move_iterator1: {104,104} == move_begin: {100,100} false
move_iterator1: {104,104} != move_begin: {100,100} true
move_iterator1: {104,104} != move_begin: {100,100} true
move_iterator1: {104,104} > move_begin: {100,100} true
move_iterator1: {104,104} >= move_begin: {100,100} true
move_iterator1: {104,104} < move_begin: {100,100} false
move_iterator1: {104,104} <= move_begin: {100,100} false