迭代器库-迭代器原语
迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。
迭代器分类
迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。
迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。
迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。
创建拥有从实参推出的类型的 std::move_iterator
std::make_move_iterator
|---------------------------------------------------------------------------------------------------------|---|---------------------|
| template< class Iterator > std::move_iterator<Iterator> make_move_iterator( const Iterator& i ); | | (C++11 起) (C++14 前) |
| template< class Iterator > std::move_iterator<Iterator> make_move_iterator( Iterator i ); | | (C++14 起) (C++17 前) |
| template< class Iterator > constexpr std::move_iterator<Iterator> make_move_iterator( Iterator i ); | | (C++17 起) |
make_move_iterator
是对给定迭代器 i
构造类型从参数类型推出的 std::move_iterator 的便利函数模板。
参数
|---|---|----------------|
| i | - | 转换成移动迭代器的输入迭代器 |
返回值
可用于从通过 i
访问的元素移动的 std::move_iterator
可能的实现
template< class Iterator >
std::move_iterator<Iterator> make_move_iterator( Iterator i )
{
return std::move_iterator<Iterator>(i);
}
调用示例
#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(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());
alg(std::make_move_iterator(deque1.begin()), std::make_move_iterator(deque1.end()));
// copy
std::deque<Cell> deque2(deque1.begin(), deque1.end());
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;
std::deque<Cell> deque3(std::make_move_iterator(deque2.begin()), std::make_move_iterator(deque2.end()));
std::cout << "deque3 : ";
std::copy(deque3.begin(), deque3.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
return 0;
}
输出
deque1 : {109,109} {100,100} {108,108} {108,108} {105,105} {103,103}
alg() called for random-access iterator
alg() called for random-access iterator
deque2 : {109,109} {100,100} {108,108} {108,108} {105,105} {103,103}
deque1 : {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}
deque3 : {109,109} {100,100} {108,108} {108,108} {105,105} {103,103}
deque2 : {109,109} {100,100} {108,108} {108,108} {105,105} {103,103}