文章目錄
注:轉載請標明原文出處鏈接:https://xiongyiming.blog.csdn.net/article/details/100782723
4 模板函數與模板類
4.1 函數模板
如下圖所示,函數除了數據類型不同,其他全部相同,這樣寫,太傅哦與繁瑣,因此引出函數模板 。
模板所用到的關鍵字: template, typename, class
(1) 通過 class定義模板
(2) 通過 typename定義模板
(3) 變量作爲模板參數
(4) 多參數模板
注: typename 和class可以混用
(5) 模板函數重載
代碼示例
#include<iostream>
using namespace std;
/*************************
定義函數模板display()
**************************/
template<typename T>
void display(T a)
{
cout << "a = " << a << endl;
}
template<typename T,class S>
void display(T t,S s)
{
cout << "a = " << t << endl;
cout << "s = " << s << endl;
}
template<typename T, int KSize>
void display(T a)
{
for (int i = 0; i < KSize; i++)
{
cout << "a = " << a << endl;
}
}
int main()
{
display(10);
display<int>(10);
cout << "----------------" << endl;
display<int, double>(5, 6.6);
cout << "----------------" << endl;
display<int, 5>(8);;
cin.get();
return 0;
}
運行結果
4.2 類模板
(1) 類模板
(2) 多參數類模板
注意事項:
(1) 模板代碼不能分離編譯(將聲明和定義都寫在.h文件中)
(2) 模板類中的函數在外部定義市,都要加入模板的關鍵字(參見代碼示例)。
代碼示例
MyArray.h
#pragma once
#include<iostream>
using namespace std;
template<typename T, int KSize,int KVal>
class MyArry
{
public:
MyArry();//構造函數
//析構函數
~MyArry()
{
delete[]m_pArr;
m_pArr = NULL;
}
void display();
private:
T *m_pArr;
};
//構造函數
template<typename T, int KSize, int KVal>
MyArry<T, KSize, KVal>::MyArry()
{
m_pArr = new T[KSize];
for (int i = 0; i < KSize; i++)
{
m_pArr[i] = KVal;
}
}
//成員函數
template<typename T, int KSize, int KVal>
void MyArry<T, KSize, KVal>::display()
{
for (int i= 0; i < KSize; i++)
{
cout << m_pArr[i] << endl;
}
}
main.cpp
#include<iostream>
#include"MyArray.h"
using namespace std;
/*************************
定義類模板MyArray
成員函數:構造函數 析構函數 display()函數
數據成員:m_pArr
**************************/
int main()
{
MyArry<int, 5, 6>arr;
arr.display();
cin.get();
return 0;
}
運行結果
5 標準模板庫(STL)
標準模板庫 (Standard Template Library, STL) 是惠普實驗室開發的一系列軟件的統稱。它是由Alexander Stepanov、Meng Lee和David R Musser在惠普實驗室工作時所開發出來的。雖說它主要表出現到C++中,但在被引入C++之前該技術就已經存在了很長時間。STL的代碼從廣義上講分爲三類:algorithm(算法)、container(容器)和iterator(迭代器),幾乎所有的代碼都採用了模板類和模板函數的方式,這相比於傳統的由函數和類組成的庫來說提供了更好的代碼重用機會。
(以上來自百度百科)
5.1 向量(vector)
向量(vector) 的本質是對數組的封裝。
vector特點:
(1) 根據存儲元素的個數自動變成長或縮短;
(2) 讀取數據能在常數時間內完成.
注:vector可以使用 迭代器(iterator) 進行遍歷
5.2 鏈表(list)
特點:數據插入速度快
注:list只能使用迭代器進行訪問
5.3 映射(map)
映射存儲的數據都是成對出現的。
注:map需要與pair一起使用,用來存儲多個key-value對。
代碼示例
(1) vector
#include<iostream>
#include<vector>
using namespace std;
/*************************
vector
**************************/
int main()
{
vector<int>vec;
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
cout << "vec.size=" << vec.size() << endl;
vec.pop_back();
cout << "vec.size=" << vec.size() << endl;
cout << "---------------" << endl;
//遍歷
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << endl;
}
cout << "---------------" << endl;
//使用迭代器進行遍歷
vector<int>::iterator itor = vec.begin();
cout << *itor << endl;
cout << "---------------" << endl;
for (; itor != vec.end(); itor++)
{
cout << *itor << endl;
}
cout << "---------------" << endl;
cout << vec.front() << endl;
cout << "---------------" << endl;
cout << vec.back() << endl;
cin.get();
return 0;
}
運行結果
(2) list
#include<iostream>
#include<list>
using namespace std;
/*************************
list
**************************/
int main()
{
list<int> list1;
list1.push_back(4);
list1.push_back(5);
list1.push_back(6);
//list只能使用迭代器進行訪問
list<int>::iterator itor = list1.begin();
for (; itor != list1.end(); itor++)
{
cout << *itor << endl;
}
cout << "--------------" << endl;
cin.get();
return 0;
}
運行結果
(3) map
#include<iostream>
#include<map>
#include<string>
using namespace std;
/*************************
map
**************************/
int main()
{
map<int, string> m;
pair<int, string> p1(3, "hello");
pair<int, string> p2(4, "world");
pair<int, string> p3(5, "beijing");
m.insert(p1);
m.insert(p2);
m.insert(p3);
cout << "m[3]=" << m[3] << endl;
cout << "m[4]=" << m[4] << endl;
cout << "m[5]=" << m[5] << endl;
cout << "--------------" << endl;
//map只能使用迭代器進行遍歷
map<int, string>::iterator itor = m.begin();
for (; itor != m.end(); itor++)
{
cout << itor->first << endl;
cout << itor->second << endl;
cout << endl;
}
cout << "--------------" << endl;
cin.get();
return 0;
}
參考資料
[1] C++遠征之模板篇