helper function 辅助函数。
1. 接口意义上更为直观
#include <deque> // for deque
#include <iterator> // for back_insert_iterator
#include <algorithm> // copy
std::deque<int> coll {0, 1, 2, 3, 4, 5};
std::copy(coll.begin(), coll.end(), std::back_insert_iterator<std::deque<int>>(coll));
// std::back_insert_iterator<std::deque<int>>(coll):调用该类的构造函数实例化对象
// copy 接口对第三个参数的要求为 *out = *s
// 解引用和赋值
显然不如如下的代码来的直观和简洁:
std::copy(coll.begin(), coll.end(), std::back_inserter(coll));
- (1)
std::back_insert_iterator<std::deque<int>>(coll)
构造函数,返回一个对象 (2)
std::back_inserter()
返回的恰恰也是这样一个对象template<class _Container> inline back_insert_iterator<_Container> back_inserter(_Container& _Cont) { // return a back_insert_iterator return (_STD back_insert_iterator<_Container>(_Cont)); }
2. 辅助函数是函数,利用函数的类型推导特性
还是上文的代码:
std::copy(coll.begin(), coll.end(), std::back_insert_iterator<std::deque<int>>(coll));
std::copy(coll.begin(), coll.end(), std::back_inserter(coll));