zoukankan      html  css  js  c++  java
  • C++学习笔记之模板篇

    一、模板

    不管是函数模板还是类模板,在未初始化前都是不占用内存的。 另外一般来说模板代码不能分开编译,即不能分开写成.h文件和.c文件,需要写成一个文件。

    函数模板

    关键字:

    • template:用于声明模板
    • typenameclass:声明类型,作用相同,不过建议用typename

    1.类型作为模板参数
    举个例子:

    template <class T>
    T max(T a,T b)
    {
    	return (a>b)?a:b;
    }
    
    int ival = max<int>(100,99);
    

    2.变量作为模板参数
    举个例子:

    template <int size>
    void display()
    {
    	cout<<size<<endl;
    }
    
    display<10>();
    

    3.多参数函数模板
    举个例子

    template <typename T,typename C>
    void display(T t,C c)
    {
    	cout<<t<<c<<endl;
    }
    
    int a = 666;
    string str = "marsggbo";
    dispaly<int,string>(a,str);
    

    4.数据类型和变量混用
    还是举个栗子:

    template <typename T,int size>
    void display(T t)
    {
    	int i = 0;
    	while(i++<size)
    	{
    		cout<<t<<endl;
    	}
    }
    
    dispaly<int,6>(6);
    

    类模板

    template <typename T>
    class A
    {
    public:
        A(T a);
        T display()
        {
            return A.a;
        }
    private:
        T a;
    }
    

    每定义一个成员函数时都需要在前面加上template关键字,另外在类名后还需要加上类型,即 <T> ,举个栗子:

    template <typename T>
    A::A(T x)
    {
        A.a = x;
    }
    
    template <typename T>
    T A<T>::display()
    {
    ...
    }
    

    实例化类模板

    int main(void)
    {
        A<int> test;
        test.display();
        
        return 0;
    }
    

    二、标准模板库

    1. vector

    • vector初始化

    vector初始化

    • vector常用函数

    vector常用函数
    特别注意,end() 返回的是向量迭代器末元素的下一个元素,所以如果要访问最后一个元素,表达式是这样的: *(--vector.end()) ,(ps:注意前面有个*号)

    代码示例:
    记得引入vector头文件

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main (void)
    {
        vector<int> vec(6,6);   // 初始化为6个6
        vec.push_back(5);   // 在末尾插入一个数据
        cout<<vec.size()<<endl; //vector的长度(数据数量)
        vec.pop_back();     // 删除末尾的一个数据
        cout<<vec.size()<<endl;
    
        // 遍历
        for(int i=0;i<vec.size();i++)
        {
            cout<<vec[i]<<endl;
        }
        return 0;
    }
    >>>
    7
    6
    6
    6
    6
    6
    6
    6
    
    

    2.迭代器

    迭代器相当于指针,要想访问数据,需要加上*
    直接看栗子吧

    int main(void)
    {
        vector<string> vec(2,"hello ");
        vec.push_back("marsggbo");
        vector<string>::iterator citer = vec.begin();
        cout<<*(--vec.end())<<endl; 
        for(;citer!=vec.end();citer++){
        	cout<< *citer << endl;
        }
        	
        return 0;
    } 
    >>>
    marsggbo
    hello
    hello
    marsggbo
    

    3.链表list

    注意链表遍历的时候不能使用取下标的方式,只能通过迭代器进行遍历。

    int main(void)
    {
    	list<int> t;
    	t.push_back(6);
    	t.push_back(65);
    	t.push_back(66);
    		
    	list<int>::iterator itor = t.begin();
    	for(;itor!=t.end();itor++)
    	{
    		cout<<*itor<<endl;
    	}
    	return 0;
    } 
    

    4.映射map

    这个有点类似于Python中的字典。使用的关键字是mappair

    map
    使用示例:

    int main(void)
    {
    	map<int,string> m;
    	pair<int ,string> p1(666,"marsggbo");
    	pair<int ,string> p2(222,"hexin");
    	m.insert(p1);
    	m.insert(p2);
    	
    	cout<<m[666]<<endl;
    	cout<<m[222]<<endl;		
    	return 0;
    } 
    >>>
    marsggbo
    hexin
    

    map的遍历方法:(还是以上面的例子做基础)

    int main(void)
    {
    	map<int,string> m;
    	pair<int ,string> p1(666,"marsggbo");
    	pair<int ,string> p2(222,"hexin");
    	m.insert(p1);
    	m.insert(p2);
    	
    	map<int,string>::iterator itor = m.begin();
    	for(;itor!=m.end();itor++)
    	{
    	    cout<< itor->first <<":";     // 输出键
    	    cout<< itor->second << endl;    // 输出值
    	}
    } 
    
    >>>
    222:hexin
    666:marsggbo
    

    MARSGGBO原创

    2017-4-6

  • 相关阅读:
    TokenType ([{}{}()])[{}]{}
    C# netcore 开发WebService(SoapCore)
    C++求快速幂
    二分法与牛顿迭代法求方程根
    Obtaining a Thorough CS Background Online (线上CS深度学习攻略)
    Elasticsearch 堆空间配置
    S家lic
    如何用calibredrv 来merge多个cell的gds
    siliconsmart feature
    openwrt的IPTV配置
  • 原文地址:https://www.cnblogs.com/marsggbo/p/6677057.html
Copyright © 2011-2022 走看看