标签:style blog class c code ext
template <class Container> inline back_insert_iterator<Container> back_inserter(Container& x) { return back_insert_iterator<Container>(x); // 以容器为参数,创建迭代器适配器 }
template <class Container> class back_insert_iterator { protected: Container* container; // 注意,这里是一个指向容器的指针 public: typedef output_iterator_tag iterator_category; // 输出迭代器,只支持自增 typedef void value_type; typedef void difference_type; typedef void pointer; typedef void reference; explicit back_insert_iterator(Container& x) : container(&x) {} // 与容器相绑定 back_insert_iterator<Container>& operator=(const typename Container::value_type& value) { container->push_back(value); return *this; } back_insert_iterator<Container>& operator*() { return *this; } back_insert_iterator<Container>& operator++() { return *this; } back_insert_iterator<Container>& operator++(int) { return *this; } };
#include <iostream> #include <vector> using namespace std; template <class Container> class my_back_insert_iterator { protected: Container* container; public: explicit my_back_insert_iterator(Container& x) : container(&x) {} my_back_insert_iterator<Container>& operator=(const typename Container::value_type& value) { container->push_back(value); return *this; } }; int main() { vector<int> vec; my_back_insert_iterator< vector<int> > back_insert(vec); back_insert = 1; back_insert = 2; back_insert = 3; back_insert = 4; vector<int>::iterator iter = vec.begin(); for ( ; iter != vec.end(); ++iter) cout << *iter << endl; return 0; }
reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); }
Iterator current; // 保存传入的迭代器 .... typedef Iterator iterator_type; typedef reverse_iterator<Iterator> self; explicit reverse_iterator(iterator_type x) : current(x) {} // 构造函数
reference operator*() const { Iterator tmp = current; return *--tmp; // 先自减,再接引用 } self& operator++() { --current; return *this; } self& operator--() { ++current; return *this; }
int a[] = {9,5,4,8,3,6,7}; reverse_iterator<int*> reverite(a+7); cout << *reverite++ << " "; cout << *reverite++ << " "; cout << *reverite++ << " ";
int a[] = {9,5,4,8,3,6,7}; ostream_iterator<int> outite(cout, " "); // 输出数组元素时附带输出空格 copy(a, a+7, outite);
template <class T> class ostream_iterator { protected: ostream* stream; // 绑定的标准输出 const char* string; // 分隔符 public: typedef output_iterator_tag iterator_category; typedef void value_type; typedef void difference_type; typedef void pointer; typedef void reference; ostream_iterator(ostream& s) : stream(&s), string(0) {} ostream_iterator(ostream& s, const char* c) : stream(&s), string(c) {} ostream_iterator<T>& operator=(const T& value) { *stream << value; // 关键点 if (string) *stream << string; // 输出间隔符 return *this; } // 以下三个操作都返回自己 ostream_iterator<T>& operator*() { return *this; } ostream_iterator<T>& operator++() { return *this; } ostream_iterator<T>& operator++(int) { return *this; } };
// 计算容器中小于等于3的元素个数 cout << count_if(vec.begin(), vec.end(), bind2nd(less_equal<int>(), 3));
template <class InputIterator, class Predicate, class Size> void count_if(InputIterator first, InputIterator last, Predicate pred, Size& n) { for ( ; first != last; ++first) if (pred(*first)) /* 对每个元素调用仿函数 */ ++n; /* 满足条件则累加 */ }
template <class Operation> class binder2nd : public unary_function<typename Operation::first_argument_type, typename Operation::result_type> { protected: Operation op; typename Operation::second_argument_type value; public: binder2nd(const Operation& x, // 仿函数 const typename Operation::second_argument_type& y) // 绑定的第二个数 : op(x), value(y) {} typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const { return op(x, value); // 关键点 } };
STL适配器(adapters),布布扣,bubuko.com
标签:style blog class c code ext
原文地址:http://blog.csdn.net/nestler/article/details/26560655