首页 > 其他分享 >实验六 模板类和文件IO

实验六 模板类和文件IO

时间:2022-12-06 18:55:57浏览次数:33  
标签:index int Vector 实验 IO output include 模板 size

1.1 Vector.hpp:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

template <class T>
class Vector
{
public:
    Vector(int n) : size{n}
    {
        p = new T[size];
    }
    Vector(int n, T value) : size{n}
    {
        p = new T[size];
        int i;
        for (i = 0; i < size; i++)
            p[i] = value;
    }
    ~Vector()
    {
        delete[] p;
    }

    T &operator[](int index)
    {
        if (index >= 0 && index < size)
            return p[index];
    }

    T &at(int index)
    {
        if (index >= 0 && index < size)
            return p[index];
    }

    Vector(const Vector<T> &v1) : size{v1.size}
    {
        p = new T[size];
        int i;
        for (i = 0; i < size; i++)
            p[i] = v1.p[i];
    }

    int get_size() const { return size; }

    friend void output(Vector<T> &v)
    {
        int i;
        for (i = 0; i < v.size; i++)
            cout << v.p[i] << " ";
        cout << endl;
    }

private:
    int size;
    T *p;
};

1.2 task4.cpp:

#include <iostream>
#include "Vector.hpp"

void test()
{
    using namespace std;

    int n;
    while(cin >> n){
    Vector<double> x1(n);
    for (auto i = 0; i < n; ++i)
        x1.at(i) = i * 0.7;

    output(x1);

    Vector<int> x2(n, 42);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;
    output(x2);

    x3[0] = 999;
    output(x3);
    }
    cin.clear();
}

int main()
{
    test();
}

1.3 运行结果:

 

2.1 task5.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
void output(ostream &out)
{
    int n = 26;
    out << "  ";
    for (auto i = 'a'; i <= 'z'; i++)
    {
        out << setw(3) << i;
    }
    out << endl;

    for (int i = 1; i <= 26; i++)
    {
        out << setw(3) << i;
        n++;
        for (int j = n; j < n + 26; j++)
        {
            int ch = j % 26 + 65;
            out << setw(2) << char(ch);
        }
        out << endl;
    }
}
int main()
{
    ofstream out;
    output(cout);
    out.open("cipher_key.txt");
    output(out);
    out.close();
}

2.2 运行结果:

2.3 cipher_key运行结果:

 

标签:index,int,Vector,实验,IO,output,include,模板,size
From: https://www.cnblogs.com/jyksblog/p/16959601.html

相关文章

  • 实验6
    实验四vector.hpp#pragmaonce#include<iostream>usingnamespacestd;template<typenameT>classVector{public:Vector(intn):size{n}{p=newT[n......
  • Spring Session for Apache Geode 教程
    1.简介Spring会话提供了用于管理用户会话信息的API和实现。它还提供与以下各项的透明集成:HttpSession-使它能够被集群化(即复制)实现高可用性),而无需绑定到特定于应用程......
  • Python异步爬虫(aiohttp版)
    异步协程不太了解的话可以去看我上篇博客:https://www.cnblogs.com/Red-Sun/p/16934843.htmlPS:本博客是个人笔记分享,不需要扫码加群或必须关注什么的(如果外站需要加群或关......
  • 详解redis网络IO模型
    前言"redis是单线程的"这句话我们耳熟能详。但它有一定的前提,redis整个服务不可能只用到一个线程完成所有工作,它还有持久化、key过期删除、集群管理等其它模块,redis会通......
  • <五>模板的完全特例化和非完全特例化
    模板作为C++泛型编程的基础十分重要,其使得一份代码能用于处理多种数据类型。而有些时候,我们会希望对一些特定的数据类型执行不同的代码,这时就需要使用模板特例化(template......
  • C++的region代码块折叠
    之前用C#,有比较方便的#region功能:#region代码块名//...代码块#endregion 原来C++也有类似功能,示例如下:#pragmaregion代码块名//...#pragmaendregion......
  • options请求有什么用
    平常发现项目中浏览器好多个请求都会预先发起一个options类型的请求,没有返回数据。那么这个请求有什么用? options请求就是预检请求,可用于检测服务器允许的http方法。......
  • differential equations in linear algebra
    differentialequationsinlinearalgebra标签(空格分隔):线性代数目录differentialequationsinlinearalgebra1.Idea2.differentialeqautionsinalgebra3.firstord......
  • android开发新版Android studio使用新版logcat是过滤条件规则
    有四个常用的过滤关键字,分别是tag,package,level,message,line前面四个关键字过滤可以单独使用也可以组合使用最后那个line应该是指整行的意思,范围包括前面四个-tag:ex......
  • docker (56) Recv failure: Connection reset by peer
    第一步:检查防火墙是否关闭systemctlstatusfirewalld检查防火墙状态systemctldisablefirewalld永久关闭防火墙第二步:检查转发规则是否为1输入命令:sysctlnet.ipv......