[C/C++]模板函数与模板类

模板函数

#include <iostream>
#include <stdlib.h>
using namespace std;

/*
	函数模板
		要求定义函数模板display
*/

template <typename T>

void display(T t) {
	cout << t << endl;
}

template <typename T, class S>

void display(T t, S s)
{
	cout << t << endl;
	cout << s << endl;
}

template <typename T, int KSize>

void display(T t)
{
	for (int i = 0; i < KSize; i++)
	{
		cout << t << endl;
	}
}


int main()
{
	display<double>(10.12);

	display<int, double>(20, 11.32);

	display<int, 3>(20);

	system("pause");
	return 0;
}

类模板

MyArray.h
#pragma once
template <typename T, int KSize, int KVal>
class MyArray
{
public:
	MyArray();
	~MyArray()
	{
		delete[]m_pArr;
		m_pArr = NULL;
	}
	void display();
private:
	T *m_pArr;
};

template <typename T, int KSize, int KVal>
MyArray<T, KSize, KVal>::MyArray()
{
	m_pArr = new T[KSize];
	for (int i = 0; i < KSize; i++)
	{
		m_pArr[i] = KVal;
	}
}

template <typename T, int KSize, int KVal>
void MyArray<T, KSize, KVal>::display()
{
	for (int i = 0; i < KSize; i++)
	{
		cout << m_pArr[i] << endl;
	}
}
MyArray.cpp
#include "MyArray.h"


/*
MyArray::MyArray()
{
}


MyArray::~MyArray()
{
}
*/
main.cpp
#include <iostream>
#include <stdlib.h>
#include "MyArray.h"	
using namespace std;


int main()
{
	MyArray<int, 5, 6> arr;
	arr.display();

#if 0
	6
	6
	6
	6
	6
#endif

	system("pause");
	return 0;
}

练习题2:

定义一个矩形类模板

该模板中含有计算矩形面积和周长的成员函数

数据成员为矩形的长和宽。

#include <iostream>
using namespace std;

/**
 * 定义一个矩形类模板Rect
 * 成员函数:calcArea()、calePerimeter()
 * 数据成员:m_length、m_height
 */
template <typename T>
class Rect
{
public:
   Rect(T length, T height);
   T calcArea();
   T calePerimeter();
public:
	T m_length;
	T m_height;
};

/**
 * 类属性赋值
 */
template <typename T>
Rect<T>::Rect(T length, T height)
{
	m_length = length;
	m_height = height;
}

/**
 * 面积方法实现
 */
template <typename T>
T Rect<T>::calcArea()
{
	return m_length * m_height;
}

/**
 * 周长方法实现
 */
template <typename T>
T Rect<T>::calePerimeter()
{
	return ( m_length + m_height) * 2;
}

int main(void)
{
	Rect<int> rect(3, 6);
	cout << rect.calcArea() << endl;
	cout << rect.calePerimeter() << endl;
	return 0;
}

C++标准模板库 STL

向量:对数组的封装


迭代器:iterator

list:链表

map:映射


vector 例子
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <list>
#include <map>
using namespace std;


int main()
{
	vector<int> vec;
	vec.push_back(3);
	vec.push_back(4);
	vec.push_back(5);
	//vec.pop_back();
	//cout << vec.size() << endl;

	/* 遍历1
	for (int i = 0; i < vec.size(); i++)
	{
		cout << vec[i] << endl;
	}*/

	// 遍历2
	vector<int>::iterator itor = vec.begin();
	for (; itor != vec.end(); itor++)
	{
		cout << *itor << endl;
	}

	cout << vec.front() << endl;
	cout << vec.back() << endl;

	system("pause");
	return 0;
}

list例子
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <list>
#include <map>
using namespace std;


int main()
{
	list<int> list1;
	list1.push_back(3);
	list1.push_back(4);
	list1.push_back(5);
	//list1.pop_back();
	//cout << list1.size() << endl;

	list<int>::iterator itor = list1.begin();
	for (; itor != list1.end(); itor++)
	{
		cout << *itor << endl;
	}

	cout << list1.front() << endl;
	cout << list1.back() << endl;

	system("pause");
	return 0;
}

map例子
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <list>
#include <map>
#include <string>
using namespace std;


int main()
{
	map<string, string> m;
	pair<string, string> p1("h", "hello");
	pair<string, string> p2("w", "world");
	m.insert(p1);
	m.insert(p2);

	//cout << m["h"] << endl;
	//cout << m["w"] << endl;

	map<string, string>::iterator itor = m.begin();
	for (;itor != m.end(); itor++)
	{
		cout << itor->first << endl;
		cout << itor->second << endl;
		cout << endl;
	}


	system("pause");
	return 0;
}

综合练习:
使用vector存储数字3,6,8,4,并遍历。
使用map存储S-Shang Hai   B-Bei Jing    G-Guang Zhou,并遍历

#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;

int main(void)
{
    // 使用vector存储数字:3、4、8、4
    vector<int> vec;
    vec.push_back(3);
    vec.push_back(6);
    vec.push_back(8);
    vec.push_back(4);
    
    //循环打印数字
    for(int i = 0; i<vec.size(); i++)
    {
        cout << vec[i] << endl;
    }
    
    // 使用map来存储字符串键值对
    map<string, string> m;
   pair<string,string> p1("S-Shang","Hai");
   pair<string,string> p2("B-Bei","Jing");
   pair<string,string> p3("G-Guang","Zhou");

   m.insert(p1);
   m.insert(p2);
   m.insert(p3);

   map<string,string>::iterator itor = m.begin();
    // 打印map中数据
   for(;itor != m.end(); itor++)
   {
       cout << itor->first <<endl;
       cout << itor->second <<endl;
   }
    return 0;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值