C++模板及標(biāo)準(zhǔn)模板庫(kù)

模板

  • 模板是C++語(yǔ)言相對(duì)較新的一個(gè)重要特性。
    模板使程序員能夠快速建立具有類型安全的類庫(kù)集合和函數(shù)集合,它的實(shí)現(xiàn),方便了大規(guī)模的軟件開發(fā)。
  • 本節(jié)介紹了模板的概念、定義和使用模板的方法,通過這些介紹,使瀆者有效地把握模板,以便能正確使用C++系統(tǒng)中日漸龐大的標(biāo)準(zhǔn)模板類庫(kù)

函數(shù)模板的一般定義形式:

template<類型形式參數(shù)表> 
返回類型 FunctionName(形式參數(shù)表)
{
    //函數(shù)定義體
}

#include<iostream>
using namespace std;
template<class X>
X Max(X a,X b)
{
    return (a>b?a:b);
}
int main()
{   
    int x1=20;
    int x2=30;
    cout<<"Max int = "<<Max<int>(x1,x2)<<endl;
    double y1=22.5;
    double y2=12.5;
    cout<<"Max double = "<<Max<double>(y1,y2)<<endl;
    char z1='A';
    char z2='B';
    cout<<"Max char = "<<Max<char>(z1,z2)<<endl;
}
//結(jié)果為:
//Max int = 30
//Max double = 22.5
//Max char = B

  • 交換任一類類對(duì)象
void swap(T& a,T& b)
  {
           T temp=a;
           a = b;
           b = temp;
} 
//有了函數(shù)模板之后,重載就不必要了

類模板

類模板的作用

  1. 使用類模板使用戶可以為類聲明一種模式,使得類中的某些數(shù)據(jù)成員、某些成員函數(shù)的參數(shù)、某些成員函數(shù)的返回值,能取任意類型(包括基本類型的和用戶自定義類型)。
  2. 類模板定義:
template <模板參數(shù)表>
class 類名
{類成員聲明}

  1. 在類模板以外定義其成員函數(shù):
template <模板參數(shù)表>
類型名 類名<T>::函數(shù)名 ( 參數(shù)表 )

#include<iostream>
using namespace std;
template<class X,class Y>
class Test
{
    X m_t1;
    Y m_t2;
    public:
        Test(X t1,Y t2)
        {
            m_t1=t1;
            m_t2=t2;
        }
        void show()
        {
            cout<<"T1 = "<<m_t1<<"T2 = "<<m_t2<<endl;
        }
};
int main(int argc,char **argv)
{
    Test<int,char>t(10,'S');
    t.show();
}
//結(jié)果為:
//T1 = 10 T2 = S

#include<iostream>
using namespace std;
template<class X,class Y>
class Test
{
    X m_t1;
    Y m_t2;
    public:
        Test(X t1,Y t2)
        {
            m_t1=t1;
            m_t2=t2;
        }
        void show()
        {
            cout<<"T1 = "<<m_t1<<"T2 = "<<m_t2<<endl;
        }
        void print();
};
template<class X,class Y>
void Test<X,Y>::print()
{
    cout<<"t1 = "<<m_t1<<"t2 = "<<m_t2<<endl;
}
int main(int argc,char **argv)
{
    Test<int,char>t(10,'S');
    t.show();
    t.print();
}
//結(jié)果為:
//

類模板與模板類的區(qū)別

  1. 類模板是模板的定義,不是一個(gè)實(shí)實(shí)在在的類,定義中用到通用類型參數(shù)。
  2. 模板類是實(shí)實(shí)在在的類定義,是類模板的實(shí)例化。類定義中參數(shù)被實(shí)際類型所代替。

模板的實(shí)現(xiàn)

  • 模板的定義很特殊,由 template<…> 處理的任何東西都意味著編譯器在當(dāng)時(shí)不為它分配存儲(chǔ)空間,它一直處于等待狀態(tài)直到被一個(gè)模板實(shí)例告知。所以為了容易使用,幾乎總是在頭文件中放置全部的模板聲明和定義

標(biāo)準(zhǔn)模板類

  1. 將程序?qū)懙帽M可能通用
  2. 將算法從特定的數(shù)據(jù)結(jié)構(gòu)中抽象出來,成為通用的
  3. C++的模板為泛型程序設(shè)計(jì)奠定了關(guān)鍵的基礎(chǔ)
  4. STL是泛型程序設(shè)計(jì)的一個(gè)范例
  • 容器
  • 迭代器
  • 算法
  • 函數(shù)對(duì)象

七種基本容器

  1. 向量
  2. 雙端隊(duì)列
  3. 列表
  4. 集合
  5. 多重集合
  6. 映射
  7. 多重映射

容器的接口

  1. 通用容器運(yùn)算符
  • ==,!=,>,>=,<,<=,=
  1. 方法(函數(shù))
  • 迭代方法
    begin(),end(),rbegin(),rend()
  • 訪問方法
    size(),max_size(),swap(),empty()

順序容器的接口

  1. 插入方法
  • push_front(),push_back(),insert(),運(yùn)算符“=”
  1. 刪除方法
  • pop() ,erase(),clear()
  1. 迭代訪問方法
  • 使用迭代器
  1. 其他順序容器訪問方法(不修改訪問方法)
  • front(),back(),下標(biāo)[ ]運(yùn)算符
  1. 向量(vector)屬于順序容器,用于容納不定長(zhǎng)線性序列(即線性群體),提供對(duì)序列的快速隨機(jī)訪問(也稱直接訪問)
  2. 向量是動(dòng)態(tài)結(jié)構(gòu),它的大小不固定,可以在程序運(yùn)行時(shí)增加或減少。

初始化vector容器方法

  1. vector<elementType> v; // 創(chuàng)建一個(gè)沒有任何元素的空容器
  2. vector<elementType> v(otherVec); //調(diào)用拷貝構(gòu)造函數(shù)創(chuàng)建新容器
  3. vector<elementType> v(size); //創(chuàng)建一個(gè)大小為size的對(duì)象v,并使用默認(rèn)構(gòu)造函數(shù)初始化該向量
  4. vector<elementType> v(n,elem); //創(chuàng)建一個(gè)大小為n的容器,并使用元素elem初始化每一個(gè)元素
    5.vector<elementType> v(begin,end); //創(chuàng)建容器v,并使用(begin,end)之間的元素初始化容器

元素的插入

  1. veclist.push_back(elem); //將elem的一個(gè)拷貝插入到veclist的末尾
  2. veclist.insert(position,elem); //將elem的一個(gè)拷貝插入到指定的position的位置上
  3. veclist.insert(position,n,elem); //將elem的n個(gè)拷貝插入到由position指定的位置上
  4. veclist.insert(position,beg,end); //將從迭代器 beg至end-1 之間的元素插入到veclist 的position位置上

向量容器的使用

#include <vector>#include <algorithm>
using namespace std;
vector<int> num;// STL中的vector容器int element;
// 從標(biāo)準(zhǔn)輸入設(shè)備讀入整數(shù), // 直到輸入的是非整型數(shù)據(jù)為止
while (cin >> element)
 num.push_back(element);

//訪問容器內(nèi)的元素
  for(int i=0; i<num.size(); i++){
       cout<<num[i]<<endl;
   }
#include<iostream>
#include<vector>
using namespace std;
vector<int>v;
int main()
{
    for(int i=0;i<11;i++)
    {
        v.push_back(i);
    }
    for(int j=0;j<v.size();j++)
    {
        cout<<v[j]<<" ";
    }
    cout<<endl;
}
//結(jié)果為
//0 1 2 3 4 5 6 7 8 9 10

#include<iostream>
#include<vector>
using namespace std;
vector<int>v;
int main()
{
/*  for(int i=0;i<11;i++)
    {
        v.push_back(i);
    }*/
    int elem;
    while(cin>>elem)
    {
        v.push_back(elem);
    }
    for(int j=0;j<v.size();j++)
    {
        cout<<v[j]<<" ";
    }
    cout<<endl;
}
//手動(dòng)輸入數(shù)組的值,以英語(yǔ)字母結(jié)束,并且打印出來

  • 正向遍歷數(shù)組
#include<iostream>
#include<vector>
using namespace std;
vector<int>v;
int main()
{
    int elem;
    while(cin>>elem)
    {
        v.push_back(elem);
    }
    for(int j=0;j<v.size();j++)
    {
        cout<<v[j]<<" ";
    }
    cout<<endl;
    for(vector<int>::iterator it=v.begin();it<v.end();it++)
    {
        cout<<*it<<"\t";
    }
    cout<<endl;
}
//結(jié)果為:
//1 2 3 4 5 g
//則打印:1 2 3 4 5
//再次輸出:1\t2\t3\t4\t5

容器的反向遍歷

  1. 反向遍歷是使用迭代器 reverse_iterator
    vector<int>::reverse_iterator ri;
  1. rbegin, rend
  • 反向遍歷時(shí)使用rbegin,rend 來定位
  1. 反向遍歷迭代器的使用與普通的迭代器一樣,可以使用++在位移迭代器,使用* 運(yùn)算符來取元素
  • 反向遍歷數(shù)組
#include<iostream>
#include<vector>
using namespace std;
vector<int>v;
int main()
{
    int elem;
    while(cin>>elem)
    {
        v.push_back(elem);
    }
    for(int j=0;j<v.size();j++)
    {
        cout<<v[j]<<" ";
    }
    cout<<endl;
    for(vector<int>::reverse_iterator it=v.rbegin();it<v.rend();it++)
    {
        cout<<*it<<"\t";
    }
    cout<<endl;
}
//結(jié)果為:
//手動(dòng)輸入:1 2 3 4 5g
//則輸出1 2 3 4 5
//并且5 4 3 2 1

  1. 迭代器是面向?qū)ο蟀姹镜闹羔?/li>
  • 指針可以指向內(nèi)存中的一個(gè)地址
  • 迭代器可以指向容器中的一個(gè)位置
  1. STL的每一個(gè)容器類模版中,都定義了一組對(duì)應(yīng)的迭代器類。
  2. 使用迭代器,算法函數(shù)可以訪問容器中指定位置的元素,而無(wú)需關(guān)心元素的具體類型。
#include<iostream>
using namespace std;
#include<vector>
void show(vector<int> vi)
{
    vector<int>::iterator it;
    it=vi.begin();
    while(it!=vi.end())
    {
        cout<<*it++<<' ';
    }
    cout<<endl;
};
int main()
{
    vector<int> vi(3,90);
    show(vi);
    int a[5]={3,4,5,6,7};
    vi.insert(vi.begin(),a,a+5);
    show(vi);
    vi.push_back(100);
    show(vi);
    cout<<"size:"<<vi.size()<<endl;
    vi.assign(5,99);
    show(vi);
    cout<<"size:"<<vi.size()<<endl;
}
//結(jié)果為:
//90 90 90 
//3 4 5 6 7 90 90 90 
//3 4 5 6 7 90 90 90 100 
//size:9
//99 99 99 99 99 
//size:5

  1. vector<int>::iterator iter;
  • 這條語(yǔ)句定義了一個(gè)名為iter的變量,它的數(shù)據(jù)類型是由vector<int>定義的iterator類型。
  1. begin和end操作
  • 如果容器中有元素的話,由begin返回的迭代器指向第一個(gè)元素:vector<int>::iterator iter = ivec.begin();
  • 由end操作返回的迭代器指向vector的“末端元素的下一個(gè)”。通常稱為超出末端迭代器(off-the-end iterator) 。

const_iterator

  1. 該類型只能訪問容器內(nèi)元素,但不能改變其值
for (vector<string>::const_iterator iter = text.begin( ); 
iter != text.end( ); ++iter) cout << *iter << endl; 

  1. 對(duì)const_iterator類型解引用時(shí),則可以得到一個(gè)指向const對(duì)象的引用,如同任何常量一樣,該對(duì)象不能進(jìn)行重寫。
  2. 不要把const_iterator對(duì)象與const的iterator對(duì)象混淆起來, const的iterator 不能做自增減, 但可以對(duì)它指向的元素賦值

元素的刪除

  1. veclist.clear(); //清空容器中所有元素
  2. veclist.erase(position); //刪除position指定位置的元素
  3. veclist.erase(beg,end); //刪除從beg至end-1之間的元素
  4. veclist.pop_back(); //刪除最后一個(gè)元素

list的初始化方法

list的使用方法

元素插入

  1. L.push_back(elem); //向容器的末尾插入元素elem的拷貝
  2. L.push_front(elem); //向容器的開端插入元素elem的拷貝
  3. L.insert(position, elem); //向容器的position位置插入元素elem的拷貝
  4. L.insert(position, n, elem); //向容器的position位置上插入元素elem的n個(gè)拷貝
  5. L.insert(position, beg, end); //將迭代器beg至 end-1 指向的內(nèi)容插入到容器的position位置上
  6. L.splice(position, list); //將鏈表容器list中的元素插入到position位置上,并且清空l(shuí)ist容器
  7. L.splice(position, list, pos); //將容器list中的pos位置上的元素插入到position位置上,并將pos位置上的元素從list中移除
  8. L.splice(position, list, beg, end); //將容器list中beg 至 end-1 位置上的元素插入到position位置上,并將這些元素從list中移除

元素的刪除

  1. L.pop_back(); //刪除容器的最后一個(gè)元素
  2. L.pop_front(); //刪除容器的第一個(gè)元素
    L.clear(); //刪除容器的所有元素

L.erase(position); //刪除容器指定位置的元素
L.erase(beg, end); //刪除迭代器beg 至 end-1 之間的元素
L.remove(elem); //移除與元素elem相等的元素

#include<iostream>
using namespace std;
#include<list>
int main()
{
    int cpp[5]={3,6,1,7,5};
    int java[8]={6,4,7,8,15,2,3,9};
    int Unix[4]={5,2,6,9};
    list<int>li;
    li.insert(li.begin(),cpp,cpp+5);
    li.insert(li.begin(),java,java+8);
    li.insert(li.begin(),Unix,Unix+4);
    li.sort();
    li.unique();
    li.reverse();
    list<int>::iterator it=li.begin();
    while(it!=li.end())
    {
        cout<<*it++<<' ';
    }
    cout<<endl;
}
//結(jié)果為:
//15 9 8 7 6 5 4 3 2 1 

#include<iostream>
using namespace std;
#include<list>
int main()
{
    list<int>l1;
    int a[5]={3,4,5,6,7};
    list<int>l2(a,a+5);
    cout<<"l1.size():"<<l1.size()<<endl;
    cout<<"l2.size():"<<l2.size()<<endl;
    list<int>::iterator it;
    for(it=l2.begin();it!=l2.end();it++)
    {
        cout<<*it<<' ';
    }
    cout<<endl;
    //3,4,5,6,7
    it=l2.begin();
    it++;
    l2.erase(it);
    l2.insert(l2.begin(),100);
    l2.insert(l2.end(),200);
    //100,3,5,6,7,200
    for(it=l2.begin();it!=l2.end();it++)
    {
        cout<<*it<<' ';
    }
    cout<<endl;
}
//結(jié)果為:
//l1.size():0
//l2.size():5
//3 4 5 6 7 
//100 3 5 6 7 200 

#include<iostream>
using namespace std;
#include<map>
#include<string>
int main()
{
    //key(是唯一的)value
    map<int,string>mis;
    //(1)插入map元素
    mis.insert(make_pair(62,"東方不敗"));
    mis.insert(make_pair(32,"岳不群"));
    mis.insert(make_pair(36,"林平之"));
    //(2)插入方式 這里的20不是下標(biāo)!!!!!!
    mis[20]="勞德羅";
    map<int,string>::iterator it;
    it=mis.begin();
    //元素的位置和key相關(guān)和插入順序沒有關(guān)系
    //1.自動(dòng)排序
    while(it!=mis.end())
    {
        cout<<it->first<<":"<<it->second<<endl;
        ++it;
    }
}
//結(jié)果為:
//20:勞德羅
//32:岳不群
//36:林平之
//62:東方不敗

#include<iostream>
using namespace std;
#include<map>
#include<string>
int main()
{
    //key(是唯一的)value
    map<int,string>mis;
    //(1)插入map元素
    mis.insert(make_pair(62,"東方不敗"));
    mis.insert(make_pair(32,"岳不群"));
    mis.insert(make_pair(36,"林平之"));
    //(2)插入方式 這里的20不是下標(biāo)!!!!!!
    mis[20]="勞德羅";
    mis[36]="yyyy";
    map<int,string>::iterator it;
    it=mis.begin();
    //元素的位置和key相關(guān)和插入順序沒有關(guān)系
    //1.自動(dòng)排序
    while(it!=mis.end())
    {
        cout<<it->first<<":"<<it->second<<endl;
        ++it;
    }
}
//結(jié)果為:
//20:勞德羅
//32:岳不群
//36:yyyy
//62:東方不敗

  • 當(dāng)出現(xiàn)兩個(gè)下標(biāo)相同時(shí),用最后的一次

練習(xí)

補(bǔ)充

#include<iostream>
using namespace std;
class Test
{
    int m_t;
    public:
        Test()
        {
            
        }
        void lianxi()const
        {
            cout<<"lianxi const"<<endl;
        }
        void lianxi()
        {
            cout<<"lianxi"<<endl;
        }
};
int main()
{
    const Test t;
    t.lianxi();
    Test t1;
    t1.lianxi();
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容