T5-01
题目:
Swap
描述
用模板函数Swap实现对不同类型的数据进行交换。
并使用如下主函数测试。
int main()
{
int a1, a2;
std::cin >> a1 >> a2;
Swap(a1, a2);
std::cout << a1 << "," << a2 << std::endl;
double b1, b2;
std::cin >> b1 >> b2;
Swap(b1, b2);
std::cout << b1 << "," << b2 << std::endl;
char c1, c2;
std::cin >> c1 >> c2;
Swap(c1, c2);
std::cout << c1 << "," << c2 << std::endl;
return 0;
}
注意,本题只需要提交Swap函数代码,头文件和main函数系统已经提供。
输入
输入共三行。第一行两整数,第二行两浮点数,第三行两字符
输出
输出共三行。每一行为对应输入处理后的结果,输出的两个数用逗号隔开
2 3 1.2 2.3 a b
输出样例 1
3,2
2.3,1.2
b,a
答案:
#include<iostream>
using namespace std;
template<class t>
void Swap(t &a,t &b)
{
t temp=a;
a=b;
b=temp;
}
int main()
{
int a1, a2;
std::cin >> a1 >> a2;
Swap(a1, a2);
std::cout << a1 << "," << a2 << std::endl;
double b1, b2;
std::cin >> b1 >> b2;
Swap(b1, b2);
std::cout << b1 << "," << b2 << std::endl;
char c1, c2;
std::cin >> c1 >> c2;
Swap(c1, c2);
std::cout << c1 << "," << c2 << std::endl;
return 0;
}
知识点:
模板的简单使用
T5-02
题目:
SortFunctionTemplate
描述
用模板函数实现数组的输入、排序和输出。并使用如下主函数测试你的模板
int main()
{
const int LEN = 5;
int type;
while (std::cin >> type)
{
switch (type)
{
case 0:
{
int a1[LEN];
Input<int>(a1, LEN); Sort<int>(a1, LEN); Output<int>(a1, LEN);
break;
}
case 1:
{
char a2[LEN];
Input(a2, LEN); Sort(a2, LEN); Output(a2, LEN);
break;
}
case 2:
{
double a3[LEN];
Input(a3, LEN); Sort(a3, LEN); Output(a3, LEN);
break;
}
}
}
return 0;
}
注意:本题只提交Input,Sort, Output函数代码。其余部分系统已包含。
输入
输入包含多组测试数据。每组数据为两行,第一行整数type(0、1、2)。第二行为相应数组的5个元素。
输出
对于每一组测试数据,将其排序后在一行内输出,相邻元素逗号空格分离,最后为换行。
0 3 6 1 4 5 1 A B C B A
输出样例 1
1, 3, 4, 5, 6
A, A, B, B, C
答案:
#include<iostream>
using namespace std;
template<class t>
void Input(t arr[],int n)
{
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
}
template<class t>
void Sort(t arr[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
t temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
template<class t>
void Output(t arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i];
if(i<n-1)
cout<<", ";
}
cout<<endl;
}
int main()
{
const int LEN = 5;
int type;
while (std::cin >> type)
{
switch (type)
{
case 0:
{
int a1[LEN];
Input<int>(a1, LEN); Sort<int>(a1, LEN); Output<int>(a1, LEN);
break;
}
case 1:
{
char a2[LEN];
Input(a2, LEN); Sort(a2, LEN); Output(a2, LEN);
break;
}
case 2:
{
double a3[LEN];
Input(a3, LEN); Sort(a3, LEN); Output(a3, LEN);
break;
}
}
}
return 0;
}
知识点:
同上;
T5-03
题目:
Search
描述
设计一个模板函数,实现在一个给定的数组中查找给定数据是否存在,如果存在则输出该数据在数组中最小的下标,如果不存在,输出-1。以下为测试函数
int main()
{
int n;
std::cin >> n;
int *nValues = new int[n];
for (int i = 0; i < n; i++)
{
std::cin >> nValues[i];
}
int d;
std::cin >> d;
std::cout << Search(nValues, n, d) << std::endl;
delete[] nValues;
double f;
std::cin >> n;
double *dValues = new double[n];
for (int i = 0; i < n; i++)
{
std::cin >> dValues[i];
}
std::cin >> f;
std::cout << Search(dValues, n, f) << std::endl;
delete[] dValues;
std::cin >> n;
char *cValues = new char[n];
for (int i = 0; i < n; i++)
{
std::cin >> cValues[i];
}
char c;
std::cin >> c;
std::cout << Search(cValues, n, c) << std::endl;
delete[] cValues;
return 0;
}
输入
输入共三组数据,每组数据占三行。
第一组第一行整数n1,第二行是n1个整数,第三行待查找整数n
第二组第一行整数n2,第二行是n2个浮点数,第三行待查找浮点数d
第三组第一行整数n3,第二行是n3个字符,第三行待查找字符c
输出
对于每一组输入,如果查找数据存在,则输出其最小下标(下标从0开始计),否则输出-1。
7 1 1 2 5 8 10 13 8 5 -1.0 1.1 1.2 1000.10101 8.9 3.5 4 B J F U j
输出样例 1
4 -1 -1
提示
使用模板函数
template <class T>
int Search(const T * array, int arrayLen, const T & value)
答案:
#include<iostream>
using namespace std;
template <class T>
int Search(const T * array, int arrayLen, const T & value)
{
for(int i=0;i<arrayLen;i++)
{
if(array[i]==value)
return i;
}
return -1;
}
int main()
{
int n;
std::cin >> n;
int *nValues = new int[n];
for (int i = 0; i < n; i++)
{
std::cin >> nValues[i];
}
int d;
std::cin >> d;
std::cout << Search(nValues, n, d) << std::endl;
delete[] nValues;
double f;
std::cin >> n;
double *dValues = new double[n];
for (int i = 0; i < n; i++)
{
std::cin >> dValues[i];
}
std::cin >> f;
std::cout << Search(dValues, n, f) << std::endl;
delete[] dValues;
std::cin >> n;
char *cValues = new char[n];
for (int i = 0; i < n; i++)
{
std::cin >> cValues[i];
}
char c;
std::cin >> c;
std::cout << Search(cValues, n, c) << std::endl;
delete[] cValues;
return 0;
}
T5-04
题目:
TVector3
描述
构造一个模板类(Vector),数据成员如下:
template<typename T>
class Vector
{
private:
T x, y, z;
};
完成Vector,并用以下函数测试
int main()
{
double a, b, c;
std::cin >> a >> b >> c;
Vector<double> v1(a, b, c), v2(v1), v3, v4;
double d;
std::cin >> d;
v4 = d * v1 + v2;
std::cout << v4 <<std::endl;
Vector<double> v;
std::cin >> v;
int flag = (v4 == v);
std::cout << flag << std::endl;
return 0;
}
输入
见样例
输出
见样例
3 4 5 2.2 9.6 12.8 16
输出样例 1
9.6 12.8 16
1
答案:
#include<iostream>
#include <cmath>
using namespace std;
template<typename T>
class Vector
{
private:
T x, y, z;
public:
Vector(T a=0,T b=0,T c=0)
{
x=a;
y=b;
z=c;
}
Vector(const Vector& v)
{
x=v.x;
y=v.y;
z=v.z;
}
Vector& operator=(const Vector& v) {
if (this != &v) {
x = v.x;
y = v.y;
z = v.z;
}
return *this;
}
friend Vector operator*(double d,Vector v)
{
return Vector(d*v.x,d*v.y,d*v.z);
}
friend Vector operator+(Vector v1,Vector v2)
{
return Vector(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z);
}
friend istream & operator>>(istream &is,Vector& v)
{
is >> v.x >> v.y >> v.z;
return is;
}
friend ostream & operator<<(ostream &os,Vector& v)
{
os<< v.x<<" " << v.y <<" " <<v.z;
return os;
}
friend bool operator==(Vector v1,Vector v2)
{
// return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
const double epsilon = 1e-9; // 引入一个容忍误差
return (std::abs(v1.x - v2.x) < epsilon) &&
(std::abs(v1.y - v2.y) < epsilon) &&
(std::abs(v1.z - v2.z) < epsilon);
}
};
知识点:
- 模板类
- 运算符重载
- 友元函数
- double类型计算判断:引入容忍误差1e-9
T5-05
题目:
StackClassTemplate
描述
实现一个Stack类模板并测试这一模板.
template<class T, int SIZE = 20>
class Stack
{
private:
T array[SIZE]; //数组,用于存放栈的元素
int top; //栈顶位置(数组下标)
public:
Stack(); //构造函数,初始化栈
void Push(const T & ); //元素入栈
T Pop(); //栈顶元素出栈
void Clear(); //将栈清空
const T & Top() const; //访问栈顶元素
bool Empty() const; //测试栈是否为空
bool Full() const; //测试是否栈满
int Size(); //返回当前栈中元素个数
};
测试函数:
int main()
{
Stack<int, 10> intStack;
int n;
cin >> n; //n<=10
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
intStack.Push(temp);
}
for (int i = 0; i < n; i++)
{
cout << intStack.Top() << " ";
intStack.Pop();
}
cout<<endl;
if(intStack.Empty())
cout<<"Now, intStack is empty."<<endl;
Stack<string,5> stringStack;
stringStack.Push("One");
stringStack.Push("Two");
stringStack.Push("Three");
stringStack.Push("Four");
stringStack.Push("Five");
cout<<"There are "<<stringStack.Size()<<" elements in stringStack."<<endl;
stringStack.Clear();
if(stringStack.Empty())
cout<<"Now, there are no elements in stringStack"<<endl;
return 0;
}
输入
参考样例
输出
参考样例
3 1 2 3
输出样例 1
3 2 1 Now, intStack is empty. There are 5 elements in stringStack. Now, there are no elements in stringStack
答案:
#include<iostream>
using namespace std;
template<class T, int SIZE = 20>
class Stack
{
private:
T array[SIZE]; //数组,用于存放栈的元素
int top; //栈顶位置(数组下标)
public:
Stack() //构造函数,初始化栈
{
top=-1;
}
void Push(const T & t) //元素入栈
{
if(!Full())
{
top++;
array[top]=t;
}
}
T Pop() //栈顶元素出栈
{
if(!Empty())
{
return array[top--];
}
}
void Clear() //将栈清空
{
top=-1;
}
const T & Top() const //访问栈顶元素
{
if(top!=-1)
return array[top];
}
bool Empty() const //测试栈是否为空
{
if(top==-1)
return 1;
else
return 0;
}
bool Full() const //测试是否栈满
{
if(top==SIZE-1)
return 1;
else
return 0;
}
int Size() //返回当前栈中元素个数
{
return top+1;
}
};
知识点:
- 栈的基本概念、特征及其方法
- 使用模板实现栈
T5-06
题目:
MyQueue(选做)
描述
设计一个MyQueue类模板,类模板说明如下:
template <typename T> class MyQueue;//前置声明
template <typename T> std::ostream & operator<<(std::ostream &, const MyQueue<T> &);
template <typename T>
class QueueItem
{
public:
QueueItem(const T & t) :item(t), next(0)
{}
private:
T item; //value stored in this element
QueueItem *next; // pointer to next element in the MyQueue
friend class MyQueue<T>;//友元类
//通过友元函数重载<<运算符模板函数,要写上<<后的<Type>
friend ostream & operator<< <T> (ostream & os, const MyQueue<T> & q);
};
template <typename T>
class MyQueue
{
public:
MyQueue() : head(0), tail(0) {} // Empty MyQueue
MyQueue(const MyQueue &Q) //拷贝构造函数
:head(0), tail(0)
{ CopyElements(Q); };
~MyQueue() { Destroy(); }
MyQueue & operator=(const MyQueue &);
// return element from head of MyQueue
T & Front() { return head->item; }
const T & Front() const { return head->item; }
void Push(const T &); //add element to back of MyQueue
void Pop(); // remove element from head of MyQueue
bool Empty() const { return head == 0; }
void Display() const;
private:
QueueItem<T> *head;
QueueItem<T> *tail;
void Destroy(); //delete all the elements
void CopyElements(const MyQueue &);
//设置友元函数
friend ostream & operator<< <T> (ostream & os, const MyQueue<T> & q);
};
实现这个类模板中的成员函数,然后使用如下所示的main()函数测试这一类模板。
int main()
{
MyQueue<int> qi;
qi.Push(1);
qi.Push(2);
qi.Push(3);
qi.Push(4);
qi.Push(5);
qi.Pop();
qi.Display();
cout<<"\n";
cout<<qi;
cout<<endl;
MyQueue<int> qi2(qi);
qi2.Display();
cout<<endl;
MyQueue<int> qi3;
qi3 = qi;
cout<<qi3;
return 0;
}
输入
无
输出
见测试样例
null
输出样例 1
2 3 4 5 < 2 3 4 5 > 2 3 4 5 < 2 3 4 5 >
答案:
#include <iostream>
using namespace std;
template <typename T>
class MyQueue; // 前置声明
template <typename T>
std::ostream &operator<<(std::ostream &, const MyQueue<T> &); // 前置声明
template <typename T>
class QueueItem {
public:
QueueItem(const T &t) : item(t), next(0) {}
private:
T item; // 存储在这个元素中的值
QueueItem *next; // 指向MyQueue中下一个元素的指针
friend class MyQueue<T>; // 友元类
// 通过友元函数重载<<运算符模板函数
friend ostream &operator<< <T>(ostream &os, const MyQueue<T> &q);
};
template <typename T>
class MyQueue {
public:
MyQueue() : head(0), tail(0) {} // 空的MyQueue
MyQueue(const MyQueue &Q) // 拷贝构造函数
: head(0), tail(0)
{
CopyElements(Q);
}
~MyQueue() { Destroy(); }
MyQueue &operator=(const MyQueue &);
// 返回队列头部元素
T &Front() { return head->item; }
const T &Front() const { return head->item; }
void Push(const T &); // 添加元素到队列尾部
void Pop(); // 从队列头部移除元素
bool Empty() const { return head == 0; }
void Display() const;
private:
QueueItem<T> *head;
QueueItem<T> *tail;
void Destroy(); // 删除所有元素
void CopyElements(const MyQueue &);
// 友元函数
friend ostream &operator<< <T>(ostream &os, const MyQueue<T> &q);
};
// 成员函数实现
template <typename T>
void MyQueue<T>::Push(const T &value) {
QueueItem<T> *newItem = new QueueItem<T>(value);
if (tail) {
tail->next = newItem;
} else {
head = newItem;
}
tail = newItem;
}
template <typename T>
void MyQueue<T>::Pop() {
if (head) {
QueueItem<T> *oldHead = head;
head = head->next;
if (!head) {
tail = 0;
}
delete oldHead;
}
}
template <typename T>
void MyQueue<T>::Destroy() {
while (head) {
Pop();
}
}
template <typename T>
void MyQueue<T>::CopyElements(const MyQueue &Q) {
for (QueueItem<T> *p = Q.head; p; p = p->next) {
Push(p->item);
}
}
template <typename T>
MyQueue<T> &MyQueue<T>::operator=(const MyQueue &Q) {
if (this != &Q) {
Destroy();
CopyElements(Q);
}
return *this;
}
template <typename T>
void MyQueue<T>::Display() const {
for (QueueItem<T> *p = head; p; p = p->next) {
cout << p->item << " ";
}
}
template <typename T>
ostream &operator<<(ostream &os, const MyQueue<T> &q) {
os << "< ";
for (QueueItem<T> *p = q.head; p; p = p->next) {
os << p->item << " ";
}
os << ">";
return os;
}
知识点:
- 队