STL教程

STL(Standard Template Library)标准模版库

模版:

函数模版:

#include

#include

using namespace std;

template  //定义一个模版T,template  typename class没区别

T const& Max(T const& a,T const& b){

    return a

}

int main(int argc,const char* argv[]) {

    int i =39;

    int j =20;

    cout<<"Max(i, j): "<< endl

    double f1 =13.5;

    double f2 =20.7;

    cout<<"Max(f1, f2): "<< endl

    return 0;

}

类模版:

template //定义一个模版T

class Complex{

public:

    Complex(T a,T b){

        this->a = a;

        this->b = b;

    }

void  test(){

        printf("test,%d",a+b);

    }

private:

    T a;

    T b;

};

int main(int argc,const char* argv[]) {

//对象的定义,必须声明模板类型,因为要分配内容

    Complexa(10,20);

    a.test();

    return 0;

}

c++内置模版库:

1.vector

  vectorobj;//创建一个向量存储容器 int

  for(int i=0;i<10;i++) { //在数组最后添加数据

        obj.push_back(i);

        cout<0,1,2,3,4,5,6,7,8,9,

    }

    for(inti=0;i<5;i++){  //分5次去掉数组最后一个数据

        obj.pop_back();

    }

cout<<"\n"<

for(int i=0;i

    {

        cout<0,1,2,3,4,

    }

你可能感兴趣的:(STL教程)