您的位置:首页 > 编程语言 > C语言/C++

STL源码学习_1 简介概述,仿函数

2016-09-21 01:48 330 查看

STL概述与介绍

1.STL c++标准模版库

STL库包含了c++的基本算法与数据结构的框架,设计精巧,突出了范型编程的特点

2.STL 六大组件和运用

容器(containers)

算法(algorithms)

迭代器(iterators)

仿函数(functors)

配接器(adapters)

配置器(allocators)

在之后的学习中会简单的介绍上述源码构造以及底层实现

3.仿函数

所谓仿函数是指针对某个class进行()重载,它就成为了一个仿函数。对于一个可配接的仿函数,还需要小小努力。

//
// Created by 王若璇 on 16/9/21.
//
#include <iostream>

using namespace std;

template <class T>
//将operator()重载了,因此plus成了一个仿函数
struct Plus{
T operator()(const T& x,const T& y) const{
return x+y;
}
};
template <class T>
struct Minus{
T operator()(const T& x,const T& y) const {
return x-y;
}
};
int main(){
//以下产生仿函数对象
Plus<int> plusobj;
Minus<int> minusobj;

//以下使用仿函数

cout<<plusobj(3,5)<<endl;
cout<<minusobj(3,5)<<endl;

//以下直接产生仿函数的临时对象 并调用

cout<<Plus<int>()(4,10)<<endl;
cout<<Minus<double>()(3.5,4.1)<<endl;

return 0;
}


运行以上代码结果测试正确,仿函数类似与构造函数,但又不同,会在之后的学习中继续研究。之后学习空间配置器(allocator)。近期更新。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 c语言 源码 stl