现在的位置: 首页 > 综合 > 正文

STL源码剖析_读书笔记:第一章 STL概论和版本简介

2018年05月18日 ⁄ 综合 ⁄ 共 3754字 ⁄ 字号 评论关闭

第一章:

前言:

最近想深究一下STL的源码,主要是以前只知道对容器,迭代器,算法,函数对象等怎么使用,但却对其内部机制不甚了解。老话说得好:我们用交通工具,不是因为我们不会走路,而是我们会走路,有复用的工具在那里,因此可以使用。不仅要会用车轮,也要知道车轮内部是怎么工作的。闲话少叙,进入正题吧。

正文:

第一章主要从宏观角度介绍了STL的六大组件。

1)六大组件作用:

容器:存放数据的

迭代器:遍历容器的指针,屏蔽容器的差异性

算法:可以对容器中的数据进行诸如增删改查等操作

仿函数:本质是重载了operator()的类的对象昂

配接器:也就是所谓适配器,有3种。容器适配器是保存容器的接口,主要是queue,stack,priority_queue

            迭代适配器:主要是插入器。函数适配器,主要是对unarybinary函数对象的一些操作。

配置器:进行内存空间的管理。

 

2)六大组件关系:

算法->迭代器->容器:算法通过迭代器获取容器中的数据,进而对容器中的数据进行操作

适配器->容器:在容器上适配器,就限制了容器的某些特性。例如用栈,只能后进先出,用队列,先进先出。

容器->配置器->数据存储:容器可以通过是配置器来获取数据的储存空间

算法->仿函数:算法依靠仿函数选择某种策略,例如默认小于比较或用户自定义比较等等。

 

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

//模板的一般化
template<class I,class O>
class TestClass
{
public:
 void test(){cout<<"模板参数为:I;O"<<endl;}
};

//模板的偏特化
template<class M>
class TestClass<M*,M*>
{
public:
 void test(){cout<<"模板参数为:M*;M*"<<endl;}
};

template<class C>
class TestClass<const C*,C*>
{
public:
 void test() {cout<<"模板参数为:const C*,C*"<<endl;}
};

//仿函数与算法的搭配使用
template<typename T>
class Show
{
public:
 void operator()(const T& value)
 {
  cout<<"值为:"<<value<<endl;
 }
};

//静态常量直接在类中初始化
template<typename T>
class MachaoSalary
{
public:
 static const long _lSalary = 20000L;
 static const long _lHouse = 200L;
};

//increment,decrement,deference等操作
class MaInt
{
 friend ostream& operator<<(ostream& os,const MaInt& mi)
 {
  os<<mi._i<<endl;
  return os;
 }
public:
 MaInt(int i):_i(i){}
 
 //前置累加符:先累加,再取值
 MaInt& operator++()
 {
  ++(this->_i);
  return *this;
 }

 //后置累加符:先取当前值,再累加
 const MaInt operator++(int )//无法重载仅按返回类型区分的函数,所以加形参
 {
  MaInt temp = *this;
  ++(*this);//调用前置累加函数
  return temp;
 }

 MaInt& operator--()
 {
  --(this->_i);
  return *this;
 }

 const MaInt operator--(int )
 {
  MaInt temp = *this;
  //--(this->_i);
  --(*this);//调用前置累减函数
  return *temp;
 }

 //告诉编译器:要将const int转为非常量值
 int& operator*() const
 {
  return (int&)_i;
 }

private:
 int _i;
};

template<class InputIter,class T>
InputIter ma_find(InputIter first,InputIter last,const T& value)
{
 while(first!=last && *first!=value)
 {
  ++first;
 }
 return first;
}

template<class InputIter,class Func>
Func ma_for_each(InputIter first,InputIter last,Func f)
{
 for(;first!=last;first++)
 {
  f(first);
 }
 return f;
}

//仿函数的用法
template<class T>
class Multiply
{
public:
 T operator()(const T& lhs,const T&rhs)
 {
  return lhs*rhs;
 }
};

int main(int argc,char *argv[])
{
 cout<<"STL源码剖析:第一章STL概论与版本简介"<<endl;

 cout<<"第一回合:测试组态"<<endl;
#ifdef __sgi
 cout<<"__sgi"<<endl;
#endif
#ifdef __GNUC__
 cout<<"__GNUC__"<<endl;
 cout<<__GNUC__<<' '<<__GNUC_MINOR__<<endl;
#endif
#ifdef __STL_NO_DRAND48
 cout<<"I have defined __STL_NO_DRAND48 !"<<endl;
#else
 cout<<"It is a pity that I have not defined __STL_NO_DRAND48 !"<<endl;
#endif
#ifdef __STL__CLASS_PARTIAL_SPECIALIZATION
 cout<<"I have defined __STL_CLASS_PARTIAL_SPECIALIZATION !"<<endl;
#else
 cout<<"It is a pity that __STL_CLASS_PARTIAL_SPECIALIZATION !"<<endl;
#endif

 cout<<"第二回合:测试偏特化功能"<<endl;
 TestClass<string,long> tcObj1;
 TestClass<char,char> tcObj2;
 TestClass<const short*,short*> tcObj3;
 tcObj1.test();
 tcObj2.test();
 tcObj3.test();

 cout<<"第三回合:临时对象的产生与应用"<<endl;
 cout<<"临时对象:缺点:效率负担;构造方法:在类型名称之后加一对小括号,可指定初值;应用:仿函数与算法搭配"<<endl;
 char cArr[] = {'m','a','c','h','a','o'};
 //for_each(cArr,cArr+6,Show<char>);//算法中传入的是临时对象
 for_each(cArr,cArr+6,Show<char>());

 cout<<"第四回合:静态常量证书成员在class 内部直接初始化"<<endl;
 cout<<"他的薪水目标是:"<<MachaoSalary<int>::_lSalary<<"美元!"<<endl;
 cout<<"他的房子面积大小目标是:"<<MachaoSalary<int>::_lHouse<<"亩!"<<endl;

 cout<<"第五回合:increment;decrement;dereference操作符"<<endl;
 MaInt mi(20000);
 cout<<mi++<<endl;
 cout<<++mi<<endl;
 cout<<mi--<<endl;
 cout<<--mi<<endl;

 cout<<"第六回合:前闭后开区间表示法"<<endl;
 cout<<"迭代器所谓的last元素是:最后一个元素的下一个元素"<<endl;

 cout<<"第七回合:function call 操作符"<<endl;
 cout<<"用户指定的算法策略背后由一整组操作构成,以仿函数形式构成"<<endl;
 cout<<"函数指针缺点:不能持有状态"<<endl;
 cout<<"仿函数是指:重载了operator(),使用起来像函数的对象"<<endl;
 Multiply<long> multiply;
 cout<<"使用仿函数的值是:"<<multiply(109,4)<<endl;
 cout<<"使用临时对象的值是:"<<Multiply<short>()(1,2)<<endl;
 getchar();
 return 0;
}

 

 

抱歉!评论已关闭.