STL--模板和容器
来源:互联网 发布:公司网络管理方案 编辑:程序博客网 时间:2024/06/07 22:06
函数模板
#include <stdio.h> //int max(int a,int b)//{//return a > b ? a : b;//}////char max(char a,char b)//{//return a > b ? a : b;//}////float max(float a,float b)//{//return a > b ? a : b;//}template<typename T>//或者 template<class T>T max(T a, T b){ return a>b? a:b; }void main(){int iMax = max(3,5);//调用int max(int a, int b);char chMax = max('w','d'); //调用char max(char a, char b);float fMax = max(2.7f, 1.3f);//调用float max(float a, float b);}
类模板
#include <stdio.h> //class CMax//{//private://int m_a;//int m_b;////public://CMax(int a, int b)//{//m_a = a;//m_b = b;//}////public://int GetMax()//{//return m_a>m_b ? m_a : m_b;//}//};template<typename T>//或者template<class T>class CMax{private:T m_a;T m_b;public:CMax(T a, T b){m_a = a;m_b = b;}public:T GetMax(){return m_a>m_b ? m_a : m_b;}};void main(){CMax<int> maxInt(3,5);//需要指定类型int iMax = maxInt.GetMax();CMax<char> maxChar('w', 'd');char chMax = maxChar.GetMax();CMax<float> maxFloat(2.7f,1.3f);float fMax = maxFloat.GetMax();}
****************************************
可以定义多种类型的形参。
template<typename T1, typename T2>
class CTest
{...};
对象实例化时:
CTest testA<int, float>;
CTest testB<double, string>
- STL--模板和容器
- STL模板和容器(1)
- STL常用容器模板
- 标准模板库STL-容器
- STL模板的容器理解
- 初识STL和容器
- C++STL模板容器(一)
- C++STL模板容器(二)
- C++STL模板容器(三)
- C++STL模板容器(四)
- C模板实现STL容器中的vector
- STL(标准模板库)理论基础与容器
- 组织和容器模板
- 模板和容器
- C++标准模板库STL算法与自适应容器(栈和队列)
- STL 容器和迭代器基础
- ACM STL容器和算法
- stl-容器set和multiset
- 为了能进黑马拼了...
- Windows Embedded Compact 2013 安装体验 ----1
- 安装eclipse的操作方法
- 链表那点事
- Qt读取JSON和XML数据
- STL--模板和容器
- 基于RMAN的异机数据库克隆(rman duplicate)
- android.app.Application cannot be cast to com.mr.realcus.GlobalApplication
- CXF动态客户端调用webservice实例
- SSH远程登录无需输入密码
- 小学生玩ACM----深搜
- 新浪微博涨粉秘籍,真秘籍!!
- CommandBehavior.CloseConnection有何作用
- 串口通信协议示例与分析