safari下的jianshu根本无法直接粘贴剪贴板里的图像啊。
请到网易云课堂笔记那儿下载附件,谢谢!
一、标准容器所提供的操作和类型可以概括如下:
二、标准容器操作复杂性如下:
三、标准容器的成员类型:
四、构造函数、析构函数和赋值操作:
五、STL算法复杂性
六、find序列算法
七、search
八、sort
九、min和max
十、迭代器萃取
十一、迭代器操作
十二、下列操作对随机访问迭代器可能更高效
十三、迭代器适配器
十四、function
十五、函数适配器
十六、
反向迭代器的定义可能如下
十七、Iterator Adapter的一种可能实现如下(对的,就是作业)
A - Iterator Adapter包括以下类型:
1) Insert Iterator
2) Stream Iterator
3) Reverse Iterator
4) Move Iterator
B - Insert Inerator包括以下类型
1) insert_iterator<class _Container>
use insert(pos, val) for all containers
2) back_insert_iterator<class _Container>
use push_back(val) for:
1) vector
2) deque
3) list
4) string
3) front_insert_iterator<class _Container>
use push_front(val) for:
1) deque
2) list
3) forward_list
实现如下:
include <algorithm>
include <iostream>
include <deque>
include <list>
include <set>
include <vector>
include <iterator>
namespace CppWorkSTL {
// --------- 作业开始 ---------
// 作业要求的 insert_iterator
// --------------------- insert_iterator: 定义
template<class _Container>
class insert_iterator : public std::iterator<std::output_iterator_tag,
void, void, void, void> {
protected:
_Container *container;
typename _Container::iterator iter;
public:
typedef _Container container_type;
insert_iterator(_Container &_x, typename _Container::iterator _i)
: container(&_x), iter(_i) {}
insert_iterator &operator=(const typename _Container::value_type &_value) {
iter = container->insert(iter, _value);
++iter;
return *this;
}
ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
insert_iterator &operator=(typename _Container::value_type &&_value) {
iter = container->insert(iter, std::move(_value));
++iter;
return *this;
}
endif
insert_iterator &operator*() { return *this; }
insert_iterator &operator++() { return *this; }
insert_iterator &operator++(int) { return *this; }
};
// insert_iterator
// 提供一个 insert_iterator 以供调用
template<class _Container>
inline
insert_iterator<_Container>
inserter(_Container &_x, typename _Container::iterator _i) {
return insert_iterator<_Container>(_x, _i);
}
// --------------------- insert_iterator: 定义结束
// 题目要求的 ostram_iterator
// --------------------- ostram_iterator: 定义
template<class _Tp, class _CharT = char, class _Traits = std::char_traits<_CharT> >
class ostream_iterator
: public std::iterator<std::output_iterator_tag, void, void, void, void> {
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef std::basic_ostream<_CharT, _Traits> ostream_type;
private:
ostream_type *_out_stream;
const char_type *_delim;
public:
ostream_iterator(ostream_type &_s)
: _out_stream((&_s)), _delim(0) {}
ostream_iterator(ostream_type &_s, const _CharT *_delimiter)
: _out_stream((&_s)), _delim(_delimiter) {}
ostream_iterator &operator=(const _Tp &_value) {
*_out_stream << _value;
if (_delim)
*_out_stream << _delim;
return *this;
}
ostream_iterator &operator*() { return *this; }
ostream_iterator &operator++() { return *this; }
ostream_iterator &operator++(int) { return *this; }
};
// --------------------- ostram_iterator: 定义结束
// --------- 作业内容结束 ---------
// --------- 以下是题目之外的扩展内容 ---------
// 有出有进,istream_iterator
// --------------------- istream_iterator: 定义
template<class _Tp, class _CharT = char,
class _Traits = std::char_traits<_CharT>, class _Distance = ptrdiff_t>
class istream_iterator
: public std::iterator<std::input_iterator_tag, _Tp, _Distance, const _Tp *, const _Tp &> {
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef std::basic_istream<_CharT, _Traits> istream_type;
private:
istream_type *_in_stream;
_Tp _value;
public:
istream_iterator() : _in_stream(0), _value() {}
istream_iterator(istream_type &_s) : _in_stream((&_s)) {
if (!(*_in_stream >> _value))
_in_stream = 0;
}
const _Tp &operator*() const { return _value; }
const _Tp *operator->() const { return (&(operator*())); }
istream_iterator &operator++() {
if (!(*_in_stream >> _value))
_in_stream = 0;
return *this;
}
istream_iterator operator++(int) {
istream_iterator _t(*this);
++(*this);
return _t;
}
friend
bool operator==(const istream_iterator &_x, const istream_iterator &_y) {
return _x._in_stream == _y._in_stream;
}
friend
bool operator!=(const istream_iterator &_x, const istream_iterator &_y) { return !(_x == _y); }
};
// --------------------- istream_iterator: 定义结束
// --------------------- back_insert_iterator: 定义
template<class _Container>
class back_insert_iterator
: public std::iterator<std::output_iterator_tag,
void, void, void, void> {
protected:
_Container *container;
public:
typedef _Container container_type;
explicit back_insert_iterator(_Container &_x) : container(&_x) {}
back_insert_iterator &operator=(const typename _Container::value_type &_value_) {
container->push_back(_value_);
return *this;
}
ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
back_insert_iterator &operator=(typename _Container::value_type &&_value_) {
container->push_back(std::move(_value_));
return *this;
}
endif
back_insert_iterator &operator*() { return *this; }
back_insert_iterator &operator++() { return *this; }
back_insert_iterator operator++(int) { return *this; }
};
// 提供一个 back_insert_iterator 以供调用
template<class _Container>
inline
back_insert_iterator<_Container>
back_inserter(_Container &_x) {
return back_insert_iterator<_Container>(_x);
}
// --------------------- back_insert_iterator: 定义结束
// --------------------- front_insert_iterator: 定义
template<class _Container>
class front_insert_iterator
: public std::iterator<std::output_iterator_tag,
void, void, void, void> {
protected:
_Container *container;
public:
typedef _Container container_type;
explicit front_insert_iterator(_Container &_x) : container(&_x) {}
front_insert_iterator &operator=(const typename _Container::value_type &_value_) {
container->push_front(_value_);
return *this;
}
ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
front_insert_iterator &operator=(typename _Container::value_type &&_value_) {
container->push_front(std::move(_value_));
return *this;
}
endif
front_insert_iterator &operator*() { return *this; }
front_insert_iterator &operator++() { return *this; }
front_insert_iterator operator++(int) { return *this; }
};
// 提供一个 front_insert_iterator 以供调用
template<class _Container>
inline
front_insert_iterator<_Container>
front_inserter(_Container &_x) {
return front_insert_iterator<_Container>(_x);
}
// --------------------- front_insert_iterator: 定义结束
// --------- 扩展结束 ---------
} // namespace CppWorkSTL