电脑建立网站,二次开发收费需要高点,烟台定制网站建设公司,无锡公司网站建设服务系列文章目录
STL源码剖析笔记——迭代器 STL源码剖析笔记——vector STL源码剖析笔记——list STL源码剖析笔记——deque、stack#xff0c;queue STL源码剖析笔记——Binary Heap、priority_queue STL源码剖析笔记——AVL-tree、RB-tree、set、map、mutiset、mutimap STL源…系列文章目录
STL源码剖析笔记——迭代器 STL源码剖析笔记——vector STL源码剖析笔记——list STL源码剖析笔记——deque、stackqueue STL源码剖析笔记——Binary Heap、priority_queue STL源码剖析笔记——AVL-tree、RB-tree、set、map、mutiset、mutimap STL源码剖析笔记——哈希表、unordered_set、unordered_map、unordered_mutiset、unordered_mutimap STL源码剖析笔记——仿函数函数对象 STL源码剖析笔记——适配器adapters 文章目录 系列文章目录1. 定义2. 容器适配器3. 迭代器适配器4. 仿函数适配器1not1、not22bind1st、bind2nd3compose1、compose24ptr_fun 1. 定义 适配器Adapter是指一些特定的容器或迭代器它们提供了不同于其底层实现的接口以便与算法或其他容器进行协同工作是通过将底层实现进行改造以满足特定的需求。适配器跟底层实现的关系是包含而不是继承。 主要分为三种容器适配器迭代器适配器仿函数适配器。
2. 容器适配器 STL提供的两个容器queue和stack,其实都只不过是一种配接器。它们通过修饰底层的deque接口实现特殊的功能。从queue和stack的构造来看两者内部都包含了一个queue封住了所有的deque对外接口只开放符合queue和stack原则的几个函数。queue和stack是容器本质上也是作用于容器之上的一个适配器。
template class T, class SequencedequeT
class stack {
protected:Sequence c; //底层容器
};template class T, class Sequence dequeT
class queue {
protected:Sequence c; //底层容器
};3. 迭代器适配器 STL提供了许多应用于迭代器身上的配接器包括insert Iterators, reverse iterators, iostream iteratorsors。他们本身就是迭代器也是作用于迭代器上的一个适配器。通过修饰底层的迭代器接口实现特殊的功能以reverse iterators为例 reverse iterators的主要作用是实现反向迭代器遍历rbegin()和rend()函数都是通过它来实现。 对于一般的迭代器来说begin()指向的是第一个元素end()指向的是最后一个元素的后一个位置是[ begin(), end() ),前闭后开。现在反向迭代器应该要保持这种设定即rbegin()指向的是最后一个元素rend()指向的是第一个元素的前一个位置。而且这种功能就要通过reverse iterators适配器来实现。 可以看到reverse iterators中包含了一个正向的迭代器对这个反向迭代器进行取值的操作其实就是对内部的正向迭代器的前一位进行取值包括其他的operator,operator–,operator,operator-,其实都是根据要求对内部包含的正向迭代器进行操作。
4. 仿函数适配器 仿函数适配器functor adapters (亦称为function adapters)是所有配接器中数量最庞大的一个族群其配接灵活度也是前二者所不能及可以配接、配接、再配接。这些配接操作包括系结(bind)、否定(negate),组合(compose)、以及对一般函数或成员函数的修饰(使其成为一个仿函数)。下图为count_if()和 bind2nd (less () , 12))的搭配实例。 这个搭配想要得到数组中所有小于12的数具体流程为 1.从count_if函数中可以知道它接受两个迭代器begin()和end())以及一个函数对象将每一个迭代器取值放入函数对象中进行处理 2.less()是一个仿函数接受两个参数判断参数1是否小于参数2。
template class T
struct less:public binar_functionT, T, bool {bool operator()(const T x, const T y) const { return x y;}
};3.bind2nd内部有一个binder2nd对象binder2nd接受一个模板对象和这个模板对象的第二参数类型其operator为将输入的value绑定到模板对象的第二参数。最后将bind2nd对象传给count_if作为pred的参数实现功能。
1not1、not2 not1、not2的功能为接收一个仿函数对象作为参数并对返回值进行逻辑否定区别在于not1用于接受一元函数not2用于接受二元函数。
//辅助函数使我们得以方便使用 unary_negatePred
template class Predicate〉
inline unary_negatePredicate notl(const Predicated pred) { return unary_negatePiedicate(pred);
}//以下配接器用来表示某个Adaptable Predicate的逻辑负值 (logical negation)
template class Predicate〉
class unary_negate:public unary_functiontypename Predicate::arguement_typex bool {
protected:Predicate pred; / / 内部成员
public:explicit unary_negate(const Predicated x):pred(x) {}bool operator()(const typename Predicate::argument_type x) const { return !pred(x); / / 将pred的运算结果加上否定(negate)运算}
};//辅助函数使我们得以方便使用bineary_negatePred
templateclass Predicate
inline binary_negatePredicate not2(const Predicated pred) { return binary_negatePredicate(pred);
}//以下配接器用来表示某个Adaptable Binary Predicate的逻辑负值 template class Predicate
class binary_negate:public binary_functiontypename Predicate::first_argument_type, typename Predicate::second_argument_type, bool {
protected:Predicate pred; // 内部成员
public:explicit binary_negate(const Predicated x) : pred(x) {}bool operator()(const typename Predicate::first_argument_type const typename Predicate::second_arguinent_type y) const {return !pred(x, y); //将pred的运算结果加上否定(negate)运算}
};2bind1st、bind2nd
//辅助函数让我们得以方便使用binderlstOp template class Operation, class T
inline binder1stOperation bind1st(const Operation op, const T x) {typedef typename Operation::first_argument_type argl_type;return binderlstOperation(op, argl_type(x));//以上注意先把x转型为op的第一参数型别
}template class Operation
class binder1st:public unary_functiontypename Operation::second_argument_type, typename Operation::result_type {
protected:Operation op; // 内部成员typename Operation::first_argument_type value;// 内部成员
public:
// constructorbinder1st(const Operation x, const typename Operation::first_argument_type y):op (x),value (y) {} //将表达式和第一参数记录于内部成员typename Operation::result_typeoperator()(const typename Operation::second_argtunent_type x) const {return op (value, x); //实际调用表达式并将value绑定为第一参数
} );bind1st的功能是将一个二元函数转变为一元函数他接受一个函数对象和一个函数对象第一参数类型的值并将这个值绑定到函数对象的第一参数返回这个函数对象的返回类型。 std::lessint lessThan;std::binder1ststd::lessint lessThan5 std::bind1st(lessThan, 5);bool result lessThan5(3); // 相当于lessThan(5, 3)bind2nd与bind1st唯一区别在于bind2nd将值绑定到对象的第二参数。
3compose1、compose2
已知两个一元函数f () , g (),配接器compose1用来产生一个h(), 使得h(x) f(g(x))即 conpose1( f () , g () )的效果等于f( g(x) )
//辅助函数让我们得以方便运用 unary_composeOpl,0p2
template class Operationl, class Operation2
inline unary_composeOperationl, Operation2 composel(const Operationl opl, const Operation2 op2) {return unary_composeOperationl, Operation2(opl, op2);
}template class Operationl, class Operation2
class unary_compose:public unary_functiontypename Operatioh2::argument^type, typename Operatiohl::result_type {
protected:Operationl op1; //内部成员Operation2 op2;// 内吾B成员
public:
// constructor
unary_compose(const Operationl x, const 0peration2 y):opl (x) , op2 (y) {} / /将两个表达式记录于内部成员typename Operationl::result_typeoperator()(const typename Operations:ar0mnent_type x) const {return opl (op2 (x) );// 函数合成
};相应的已知两个一元函数f () , g ()和一个二元函数h(),配接器compose2用来产生一个F(), 使得F(x) H( f () , g () )即 conpose2( H(), f () , g () )的效果等于H( f () , g () )
4ptr_fun
//辅助函数让我们得以方便运用pointer_to_unary_function
template class Arg, class Result
inline pointer_to_unary_functionArg, Result ptr_fun(Result (*x)(Arg)) {return pointer_to_unary_functionArg,Result(x);
}//以下配接器其实就是把一个一元函数指针包起来
//当仿函数被使用时就调用该函数指针
template class Arg, class Result
class pointer_to_unary_function:public unary_functioiArg, Result {
protected:Result (*ptr) (Arg);//内部成员一个函数指针
public:pointer_to_unary_function() {}
//以下constructor将函数指针记录于内部成员之中explicit pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}
//以下通过函数指针执行函数Result operator()(Arg x) const { return ptr(x);}
};ptr_fun配接器其实就是把一个一元函数指针包起来当仿函数被使用时就调用该函数指针。