西北农林科技大学2024学年C++面向对象程序设计OJ——T14 安全数组类模板

一.题目描述

Description

设计一个安全数组类模板Array<T>,其中包含数组的输入、输出、排序和查找等方法,使用三种类型的数据对其进行测试。

(1)设计构造函数Array<T>::Array(int n),可动态分配n个T类型的存储空间;

(2)设计析构函数Array<T>::~Array()释放内存;

(3)重载输入流运算符istream &operator>>(istream& in, Array<T>& arr)读入n个T类型数据;

(4)重载输出流运算符ostream &operator<<(ostream& out, const Array<T>& arr)输出n个T类型数据;

(5)重载[]运算符,若索引值i越界,则输出"Out of boundary"并退出程序,否则返回第i个数据元素;

(6)基于<algorithm>中的sort函数定义成员函数void Array<T>::sort(),实现数组排序;

(7)设计成员函数int Array<T>::search(T e)const,若查找成功返回非负索引值,否则返回-1;

(8)设计函数模板void Process(Array<T> &a)用于测试数组类模板。

main函数对应测试代码如下:

复制代码
int main()
{
    string type;
    int n;

    cin >> type >> n;
    if (type=="int")
    {
        Array<int> a(n);
        Process(a);
    }
    else if (type=="double")
    {
        Array<double> a(n);
        Process(a);
    }
    else if (type=="string")
    {
        Array<string> a(n);
        Process(a);
    }
    else
        cout << "Input error!" << endl;

    return 0;
}

二.输入与输出

Input

数据类型type和元素个数n

n个数据元素

索引值pos

查找键值key

Output

排序前数据序列

排序后数据序列

索引值pos对应数据元素

查找到的数据元素索引值

Sample Input 1

复制代码
int 5
18 2 4 6 25
2
6

Sample Output 1

复制代码
18 2 4 6 25
2 4 6 18 25
6
2

Sample Input 2

复制代码
double 6
24.5 3.6 18.3 96.4 102.56 88.1
3
102

Sample Output 2

复制代码
24.5 3.6 18.3 96.4 102.56 88.1
3.6 18.3 24.5 88.1 96.4 102.56
88.1
-1

Sample Input 3

复制代码
string 7
dispose campus budget slip bacteria consume blast
7
campus

Sample Output 3

复制代码
dispose campus budget slip bacteria consume blast
bacteria blast budget campus consume dispose slip
Out of boundary!

三.代码

cpp 复制代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
template<typename T>
class Array{
    int size_;
    T *arr;
public:
    Array(int n):size_(n){this->arr=new T[n];}
    ~Array(){delete[] arr;}
    int getsize()const{return this->size_;}
    template<typename Ti>
    friend istream &operator>>(istream& in,Array<Ti>& arr);
    template<typename To>
    friend ostream &operator<<(ostream& in,const Array<To>& arr);
    void sort_arr(){sort(arr,arr+size_);}
    int Search(T e){
        for(int i=0;i<size_;i++){
            if(arr[i]==e)
                return i;
        }
        return -1;
    }
    T& operator[](const int index){
        if(index<0 || index>=this->size_){
            cout<<"Out of boundary!"<<endl;
            exit(0);
        }
        return this->arr[index];
    }
};
template <typename T>
void Process(Array<T> &a){
        int pos;
        T e;
        cin>>a;
        cout<<a<<endl;
        a.sort_arr();
        cout<<a<<endl;
        cin>>pos;
        cout<<a[pos]<<endl;
        cin>>e;
        cout<<a.Search(e)<<endl;
}
template<typename Ti>
istream &operator>>(istream& in,Array<Ti>& obj){
    for(int i=0;i<obj.getsize();i++)
        in>>obj.arr[i];
    return in;
}
template<typename To>
ostream &operator<<(ostream& out,const Array<To>& obj){
    int i;
    for(i=0;i<obj.getsize()-1;i++){
        	out<<obj.arr[i]<<" ";
    }
    out<<obj.arr[i];
    return out;
}
int main()
{
    string type;
    int n;

    cin >> type >> n;
    if (type=="int")
    {
        Array<int> a(n);
        Process(a);
    }
    else if (type=="double")
    {
        Array<double> a(n);
        Process(a);
    }
    else if (type=="string")
    {
        Array<string> a(n);
        Process(a);
    }
    else
        cout << "Input error!" << endl;

    return 0;
}
相关推荐
Dream it possible!43 分钟前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
爱和冰阔落1 小时前
【C++继承下】继承与友元 / static 菱形继承与虚继承 组合的详解分析
c++·面试·腾讯云ai代码助手
草莓熊Lotso2 小时前
《C++ Stack 与 Queue 完全使用指南:基础操作 + 经典场景 + 实战习题》
开发语言·c++·算法
敲上瘾2 小时前
单序列和双序列问题——动态规划
c++·算法·动态规划
ajassi20002 小时前
开源 C++ QT QML 开发(二十二)多媒体--ffmpeg编码和录像
c++·qt·开源
小糖学代码4 小时前
Linux:11.线程概念与控制
linux·服务器·c语言·开发语言·c++
Larry_Yanan7 小时前
QML学习笔记(四十)QML的ApplicationWindow和StackView
c++·笔记·qt·学习·ui
Kratzdisteln9 小时前
【C语言】Dev-C++如何编译C语言程序?从安装到运行一步到位
c语言·c++
Dream it possible!10 小时前
LeetCode 面试经典 150_栈_有效的括号(52_20_C++_简单)(栈+哈希表)
c++·leetcode·面试··哈希表
kyle~11 小时前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++